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 | |
| 11 | #ifdef SK_VULKAN |
| 12 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 13 | #include "vk/GrVkBackendContext.h" |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 14 | #include "WindowContext.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 15 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame^] | 16 | class GrRenderTarget; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 17 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 18 | namespace sk_app { |
| 19 | |
| 20 | class VulkanWindowContext : public WindowContext { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 21 | public: |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 22 | ~VulkanWindowContext() override; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 23 | |
| 24 | // each platform will have to implement these in its CPP file |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 25 | static VkSurfaceKHR createVkSurface(VkInstance, void* platformData); |
| 26 | static bool canPresent(VkInstance, VkPhysicalDevice, uint32_t queueFamilyIndex); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 27 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 28 | static VulkanWindowContext* Create(void* platformData, const DisplayParams& params) { |
| 29 | VulkanWindowContext* ctx = new VulkanWindowContext(platformData, params); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 30 | if (!ctx->isValid()) { |
| 31 | delete ctx; |
| 32 | return nullptr; |
| 33 | } |
| 34 | return ctx; |
| 35 | } |
| 36 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame^] | 37 | sk_sp<SkSurface> getBackbufferSurface() override; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 38 | void swapBuffers() override; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 39 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 40 | bool isValid() override { return SkToBool(fBackendContext.get()); } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 41 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 42 | void resize(uint32_t w, uint32_t h) override { |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 43 | this->createSwapchain(w, h, fDisplayParams); |
| 44 | } |
| 45 | |
liyuqian | 796c5bb | 2016-05-09 08:49:29 -0700 | [diff] [blame] | 46 | void setDisplayParams(const DisplayParams& params) override { |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 47 | this->createSwapchain(fWidth, fHeight, params); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 48 | } |
| 49 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 50 | GrBackendContext getBackendContext() override { |
| 51 | return (GrBackendContext) fBackendContext.get(); |
| 52 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 55 | VulkanWindowContext(void*, const DisplayParams&); |
| 56 | void initializeContext(void*, const DisplayParams&); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 57 | void destroyContext(); |
| 58 | |
| 59 | struct BackbufferInfo { |
| 60 | uint32_t fImageIndex; // image this is associated with |
| 61 | VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image |
| 62 | VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done |
| 63 | VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render |
| 64 | VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU |
| 65 | }; |
| 66 | |
| 67 | BackbufferInfo* getAvailableBackbuffer(); |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 68 | bool createSwapchain(uint32_t width, uint32_t height, const DisplayParams& params); |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 69 | void createBuffers(VkFormat format); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 70 | void destroyBuffers(); |
| 71 | |
| 72 | SkAutoTUnref<const GrVkBackendContext> fBackendContext; |
| 73 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 74 | // simple wrapper class that exists only to initialize a pointer to NULL |
| 75 | template <typename FNPTR_TYPE> class VkPtr { |
| 76 | public: |
| 77 | VkPtr() : fPtr(NULL) {} |
| 78 | VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; } |
| 79 | operator FNPTR_TYPE() const { return fPtr; } |
| 80 | private: |
| 81 | FNPTR_TYPE fPtr; |
| 82 | }; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 83 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 84 | // WSI interface functions |
| 85 | VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR; |
| 86 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR; |
| 87 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR; |
| 88 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR; |
| 89 | VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR; |
| 90 | |
| 91 | VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR; |
| 92 | VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR; |
| 93 | VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR; |
| 94 | VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR; |
| 95 | VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR; |
| 96 | VkPtr<PFN_vkCreateSharedSwapchainsKHR> fCreateSharedSwapchainsKHR; |
| 97 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 98 | VkSurfaceKHR fSurface; |
| 99 | VkSwapchainKHR fSwapchain; |
| 100 | uint32_t fPresentQueueIndex; |
| 101 | VkQueue fPresentQueue; |
| 102 | int fWidth; |
| 103 | int fHeight; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 104 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame^] | 105 | uint32_t fImageCount; |
| 106 | VkImage* fImages; // images in the swapchain |
| 107 | VkImageLayout* fImageLayouts; // layouts of these images when not color attachment |
| 108 | sk_sp<GrRenderTarget>* fRenderTargets; // wrapped rendertargets for those images |
| 109 | sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts) |
| 110 | VkCommandPool fCommandPool; |
| 111 | BackbufferInfo* fBackbuffers; |
| 112 | uint32_t fCurrentBackbufferIndex; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 115 | } // namespace sk_app |
| 116 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 117 | #endif // SK_VULKAN |
| 118 | |
| 119 | #endif |