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 | |
| 50 | VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn); |
| 51 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 52 | private: |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame^] | 53 | void initializeContext(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 54 | void destroyContext(); |
| 55 | |
| 56 | struct BackbufferInfo { |
| 57 | uint32_t fImageIndex; // image this is associated with |
| 58 | VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image |
| 59 | VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done |
| 60 | VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render |
| 61 | VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU |
| 62 | }; |
| 63 | |
| 64 | BackbufferInfo* getAvailableBackbuffer(); |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 65 | bool createSwapchain(int width, int height, const DisplayParams& params); |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 66 | void createBuffers(VkFormat format); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 67 | void destroyBuffers(); |
| 68 | |
Hal Canary | 1b612a8 | 2016-11-03 16:26:13 -0400 | [diff] [blame] | 69 | sk_sp<const GrVkBackendContext> fBackendContext; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 70 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 71 | // simple wrapper class that exists only to initialize a pointer to NULL |
| 72 | template <typename FNPTR_TYPE> class VkPtr { |
| 73 | public: |
| 74 | VkPtr() : fPtr(NULL) {} |
| 75 | VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; } |
| 76 | operator FNPTR_TYPE() const { return fPtr; } |
| 77 | private: |
| 78 | FNPTR_TYPE fPtr; |
| 79 | }; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 80 | |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame^] | 81 | // Create functions |
| 82 | CreateVkSurfaceFn fCreateVkSurfaceFn; |
| 83 | CanPresentFn fCanPresentFn; |
| 84 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 85 | // WSI interface functions |
| 86 | VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR; |
| 87 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR; |
| 88 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR; |
| 89 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR; |
| 90 | VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR; |
| 91 | |
| 92 | VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR; |
| 93 | VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR; |
| 94 | VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR; |
| 95 | VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR; |
| 96 | VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR; |
| 97 | VkPtr<PFN_vkCreateSharedSwapchainsKHR> fCreateSharedSwapchainsKHR; |
| 98 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 99 | VkSurfaceKHR fSurface; |
| 100 | VkSwapchainKHR fSwapchain; |
| 101 | uint32_t fPresentQueueIndex; |
| 102 | VkQueue fPresentQueue; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 103 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 104 | uint32_t fImageCount; |
| 105 | VkImage* fImages; // images in the swapchain |
| 106 | VkImageLayout* fImageLayouts; // layouts of these images when not color attachment |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 107 | sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts) |
| 108 | VkCommandPool fCommandPool; |
| 109 | BackbufferInfo* fBackbuffers; |
| 110 | uint32_t fCurrentBackbufferIndex; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 113 | } // namespace sk_app |
| 114 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 115 | #endif // SK_VULKAN |
| 116 | |
| 117 | #endif |