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