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 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 29 | static VulkanWindowContext* Create(void* platformData, int msaaSampleCount) { |
| 30 | VulkanWindowContext* ctx = new VulkanWindowContext(platformData, msaaSampleCount); |
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 { |
| 46 | this->createSwapchain(w, h); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 47 | } |
| 48 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 49 | GrBackendContext getBackendContext() override { |
| 50 | return (GrBackendContext) fBackendContext.get(); |
| 51 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 54 | VulkanWindowContext(); |
| 55 | VulkanWindowContext(void*, int msaaSampleCount); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 56 | void initializeContext(void*); |
| 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(); |
| 68 | bool createSwapchain(uint32_t width, uint32_t height); |
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 | GrContext* fContext; |
| 99 | VkSurfaceKHR fSurface; |
| 100 | VkSwapchainKHR fSwapchain; |
| 101 | uint32_t fPresentQueueIndex; |
| 102 | VkQueue fPresentQueue; |
| 103 | int fWidth; |
| 104 | int fHeight; |
| 105 | GrPixelConfig fPixelConfig; |
| 106 | |
| 107 | uint32_t fImageCount; |
| 108 | VkImage* fImages; // images in the swapchain |
| 109 | VkImageLayout* fImageLayouts; // layouts of these images when not color attachment |
| 110 | sk_sp<SkSurface>* fSurfaces; // wrapped surface for those images |
| 111 | VkCommandPool fCommandPool; |
| 112 | BackbufferInfo* fBackbuffers; |
| 113 | uint32_t fCurrentBackbufferIndex; |
| 114 | }; |
| 115 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 116 | } // namespace sk_app |
| 117 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 118 | #endif // SK_VULKAN |
| 119 | |
| 120 | #endif |