blob: 8480a9efa83f31e94dc483569114173d3002cae2 [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
jvanverthaf236b52016-05-20 06:01:06 -070016class GrRenderTarget;
jvanverth9f372462016-04-06 06:08:59 -070017
jvanvertha8d0d6c2016-05-05 12:32:03 -070018namespace sk_app {
19
20class VulkanWindowContext : public WindowContext {
jvanverth9f372462016-04-06 06:08:59 -070021public:
jvanvertha8d0d6c2016-05-05 12:32:03 -070022 ~VulkanWindowContext() override;
jvanverth9f372462016-04-06 06:08:59 -070023
24 // each platform will have to implement these in its CPP file
jvanverthb0d43522016-04-21 11:46:23 -070025 static VkSurfaceKHR createVkSurface(VkInstance, void* platformData);
26 static bool canPresent(VkInstance, VkPhysicalDevice, uint32_t queueFamilyIndex);
jvanverth9f372462016-04-06 06:08:59 -070027
brianosman05de2162016-05-06 13:28:57 -070028 static VulkanWindowContext* Create(void* platformData, const DisplayParams& params) {
29 VulkanWindowContext* ctx = new VulkanWindowContext(platformData, params);
jvanverth9f372462016-04-06 06:08:59 -070030 if (!ctx->isValid()) {
31 delete ctx;
32 return nullptr;
33 }
34 return ctx;
35 }
36
jvanverthaf236b52016-05-20 06:01:06 -070037 sk_sp<SkSurface> getBackbufferSurface() override;
jvanvertha8d0d6c2016-05-05 12:32:03 -070038 void swapBuffers() override;
jvanverth9f372462016-04-06 06:08:59 -070039
jvanvertha8d0d6c2016-05-05 12:32:03 -070040 bool isValid() override { return SkToBool(fBackendContext.get()); }
jvanverth9f372462016-04-06 06:08:59 -070041
jvanvertha8d0d6c2016-05-05 12:32:03 -070042 void resize(uint32_t w, uint32_t h) override {
brianosman05de2162016-05-06 13:28:57 -070043 this->createSwapchain(w, h, fDisplayParams);
44 }
45
liyuqian796c5bb2016-05-09 08:49:29 -070046 void setDisplayParams(const DisplayParams& params) override {
brianosman05de2162016-05-06 13:28:57 -070047 this->createSwapchain(fWidth, fHeight, params);
jvanverth9f372462016-04-06 06:08:59 -070048 }
49
jvanvertha8d0d6c2016-05-05 12:32:03 -070050 GrBackendContext getBackendContext() override {
51 return (GrBackendContext) fBackendContext.get();
52 }
jvanverth9f372462016-04-06 06:08:59 -070053
54private:
brianosman05de2162016-05-06 13:28:57 -070055 VulkanWindowContext(void*, const DisplayParams&);
56 void initializeContext(void*, const DisplayParams&);
jvanverth9f372462016-04-06 06:08:59 -070057 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();
brianosman05de2162016-05-06 13:28:57 -070068 bool createSwapchain(uint32_t width, uint32_t height, const DisplayParams& params);
egdaniel58a8d922016-04-21 08:03:10 -070069 void createBuffers(VkFormat format);
jvanverth9f372462016-04-06 06:08:59 -070070 void destroyBuffers();
71
72 SkAutoTUnref<const GrVkBackendContext> fBackendContext;
73
jvanverthb0d43522016-04-21 11:46:23 -070074 // 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 };
jvanvertha8d0d6c2016-05-05 12:32:03 -070083
jvanverthb0d43522016-04-21 11:46:23 -070084 // 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
jvanverth9f372462016-04-06 06:08:59 -070098 VkSurfaceKHR fSurface;
99 VkSwapchainKHR fSwapchain;
100 uint32_t fPresentQueueIndex;
101 VkQueue fPresentQueue;
102 int fWidth;
103 int fHeight;
jvanverth9f372462016-04-06 06:08:59 -0700104
jvanverthaf236b52016-05-20 06:01:06 -0700105 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;
jvanverth9f372462016-04-06 06:08:59 -0700113};
114
jvanvertha8d0d6c2016-05-05 12:32:03 -0700115} // namespace sk_app
116
jvanverth9f372462016-04-06 06:08:59 -0700117#endif // SK_VULKAN
118
119#endif