blob: e1868815655bfbc6f7caade1136066cd6a0185a1 [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001
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 */
jvanvertha8d0d6c2016-05-05 12:32:03 -07008#ifndef VulkanWindowContext_DEFINED
9#define VulkanWindowContext_DEFINED
jvanverth9f372462016-04-06 06:08:59 -070010
11#ifdef SK_VULKAN
12
jvanverth9f372462016-04-06 06:08:59 -070013#include "vk/GrVkBackendContext.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070014#include "WindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070015
16class SkSurface;
17class GrContext;
18
jvanvertha8d0d6c2016-05-05 12:32:03 -070019namespace sk_app {
20
21class VulkanWindowContext : public WindowContext {
jvanverth9f372462016-04-06 06:08:59 -070022public:
jvanvertha8d0d6c2016-05-05 12:32:03 -070023 ~VulkanWindowContext() override;
jvanverth9f372462016-04-06 06:08:59 -070024
25 // each platform will have to implement these in its CPP file
jvanverthb0d43522016-04-21 11:46:23 -070026 static VkSurfaceKHR createVkSurface(VkInstance, void* platformData);
27 static bool canPresent(VkInstance, VkPhysicalDevice, uint32_t queueFamilyIndex);
jvanverth9f372462016-04-06 06:08:59 -070028
brianosman05de2162016-05-06 13:28:57 -070029 static VulkanWindowContext* Create(void* platformData, const DisplayParams& params) {
30 VulkanWindowContext* ctx = new VulkanWindowContext(platformData, params);
jvanverth9f372462016-04-06 06:08:59 -070031 if (!ctx->isValid()) {
32 delete ctx;
33 return nullptr;
34 }
35 return ctx;
36 }
37
jvanvertha8d0d6c2016-05-05 12:32:03 -070038 SkSurface* getBackbufferSurface() override;
39 void swapBuffers() override;
jvanverth9f372462016-04-06 06:08:59 -070040
jvanvertha8d0d6c2016-05-05 12:32:03 -070041 bool makeCurrent() override { return true; }
jvanverth9f372462016-04-06 06:08:59 -070042
jvanvertha8d0d6c2016-05-05 12:32:03 -070043 bool isValid() override { return SkToBool(fBackendContext.get()); }
jvanverth9f372462016-04-06 06:08:59 -070044
jvanvertha8d0d6c2016-05-05 12:32:03 -070045 void resize(uint32_t w, uint32_t h) override {
brianosman05de2162016-05-06 13:28:57 -070046 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);
jvanverth9f372462016-04-06 06:08:59 -070052 }
53
jvanvertha8d0d6c2016-05-05 12:32:03 -070054 GrBackendContext getBackendContext() override {
55 return (GrBackendContext) fBackendContext.get();
56 }
jvanverth9f372462016-04-06 06:08:59 -070057
58private:
brianosman05de2162016-05-06 13:28:57 -070059 VulkanWindowContext(void*, const DisplayParams&);
60 void initializeContext(void*, const DisplayParams&);
jvanverth9f372462016-04-06 06:08:59 -070061 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();
brianosman05de2162016-05-06 13:28:57 -070072 bool createSwapchain(uint32_t width, uint32_t height, const DisplayParams& params);
egdaniel58a8d922016-04-21 08:03:10 -070073 void createBuffers(VkFormat format);
jvanverth9f372462016-04-06 06:08:59 -070074 void destroyBuffers();
75
76 SkAutoTUnref<const GrVkBackendContext> fBackendContext;
77
jvanverthb0d43522016-04-21 11:46:23 -070078 // 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 };
jvanvertha8d0d6c2016-05-05 12:32:03 -070087
jvanverthb0d43522016-04-21 11:46:23 -070088 // 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
jvanverth9f372462016-04-06 06:08:59 -0700102 GrContext* fContext;
103 VkSurfaceKHR fSurface;
104 VkSwapchainKHR fSwapchain;
105 uint32_t fPresentQueueIndex;
106 VkQueue fPresentQueue;
107 int fWidth;
108 int fHeight;
brianosman05de2162016-05-06 13:28:57 -0700109 DisplayParams fDisplayParams;
jvanverth9f372462016-04-06 06:08:59 -0700110 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
jvanvertha8d0d6c2016-05-05 12:32:03 -0700121} // namespace sk_app
122
jvanverth9f372462016-04-06 06:08:59 -0700123#endif // SK_VULKAN
124
125#endif