blob: 79298f6b384e932630e3ed6d465559b8e478ae74 [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
djsollen9207cae2016-06-10 07:50:00 -070011#include "SkTypes.h" // required to pull in any SkUserConfig defines
12
jvanverth9f372462016-04-06 06:08:59 -070013#ifdef SK_VULKAN
14
jvanverth9f372462016-04-06 06:08:59 -070015#include "vk/GrVkBackendContext.h"
Greg Danielf730c182018-07-02 20:15:37 +000016#include "vk/GrVkInterface.h"
17#include "vk/VkTestUtils.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070018#include "WindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070019
jvanverthaf236b52016-05-20 06:01:06 -070020class GrRenderTarget;
jvanverth9f372462016-04-06 06:08:59 -070021
jvanvertha8d0d6c2016-05-05 12:32:03 -070022namespace sk_app {
23
24class VulkanWindowContext : public WindowContext {
jvanverth9f372462016-04-06 06:08:59 -070025public:
jvanvertha8d0d6c2016-05-05 12:32:03 -070026 ~VulkanWindowContext() override;
jvanverth9f372462016-04-06 06:08:59 -070027
jvanverthaf236b52016-05-20 06:01:06 -070028 sk_sp<SkSurface> getBackbufferSurface() override;
jvanvertha8d0d6c2016-05-05 12:32:03 -070029 void swapBuffers() override;
jvanverth9f372462016-04-06 06:08:59 -070030
Greg Danielf730c182018-07-02 20:15:37 +000031 bool isValid() override { return fDevice != VK_NULL_HANDLE; }
jvanverth9f372462016-04-06 06:08:59 -070032
bsalomonccde4ab2016-07-27 08:50:12 -070033 void resize(int w, int h) override {
brianosman05de2162016-05-06 13:28:57 -070034 this->createSwapchain(w, h, fDisplayParams);
35 }
36
liyuqian796c5bb2016-05-09 08:49:29 -070037 void setDisplayParams(const DisplayParams& params) override {
Jim Van Verthfbdc0802017-05-02 16:15:53 -040038 this->destroyContext();
39 fDisplayParams = params;
40 this->initializeContext();
jvanverth9f372462016-04-06 06:08:59 -070041 }
42
bsalomond1bdd1f2016-07-26 12:02:50 -070043 /** Platform specific function that creates a VkSurfaceKHR for a window */
44 using CreateVkSurfaceFn = std::function<VkSurfaceKHR(VkInstance)>;
45 /** Platform specific function that determines whether presentation will succeed. */
Greg Danielf730c182018-07-02 20:15:37 +000046 using CanPresentFn = sk_gpu_test::CanPresentFn;
bsalomond1bdd1f2016-07-26 12:02:50 -070047
Greg Daniel35970ec2017-11-10 10:03:05 -050048 VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn,
49 PFN_vkGetInstanceProcAddr, PFN_vkGetDeviceProcAddr);
bsalomond1bdd1f2016-07-26 12:02:50 -070050
jvanverth9f372462016-04-06 06:08:59 -070051private:
Jim Van Verthfbdc0802017-05-02 16:15:53 -040052 void initializeContext();
jvanverth9f372462016-04-06 06:08:59 -070053 void destroyContext();
54
55 struct BackbufferInfo {
56 uint32_t fImageIndex; // image this is associated with
57 VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image
58 VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done
59 VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render
60 VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU
61 };
62
63 BackbufferInfo* getAvailableBackbuffer();
bsalomonccde4ab2016-07-27 08:50:12 -070064 bool createSwapchain(int width, int height, const DisplayParams& params);
Greg Danielfaa095e2017-12-19 13:15:02 -050065 void createBuffers(VkFormat format, SkColorType colorType);
jvanverth9f372462016-04-06 06:08:59 -070066 void destroyBuffers();
67
Greg Danielf730c182018-07-02 20:15:37 +000068 VkInstance fInstance = VK_NULL_HANDLE;
69 VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE;
70 VkDevice fDevice = VK_NULL_HANDLE;
jvanverth9f372462016-04-06 06:08:59 -070071
jvanverthb0d43522016-04-21 11:46:23 -070072 // simple wrapper class that exists only to initialize a pointer to NULL
73 template <typename FNPTR_TYPE> class VkPtr {
74 public:
75 VkPtr() : fPtr(NULL) {}
76 VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
77 operator FNPTR_TYPE() const { return fPtr; }
78 private:
79 FNPTR_TYPE fPtr;
80 };
jvanvertha8d0d6c2016-05-05 12:32:03 -070081
Jim Van Verthfbdc0802017-05-02 16:15:53 -040082 // Create functions
83 CreateVkSurfaceFn fCreateVkSurfaceFn;
84 CanPresentFn fCanPresentFn;
85
Greg Daniel35970ec2017-11-10 10:03:05 -050086 // Vulkan GetProcAddr functions
87 VkPtr<PFN_vkGetInstanceProcAddr> fGetInstanceProcAddr;
88 VkPtr<PFN_vkGetDeviceProcAddr> fGetDeviceProcAddr;
89
jvanverthb0d43522016-04-21 11:46:23 -070090 // WSI interface functions
91 VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR;
92 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR;
93 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR;
94 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR;
95 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR;
96
97 VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR;
98 VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR;
99 VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR;
100 VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR;
101 VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR;
Greg Danielf730c182018-07-02 20:15:37 +0000102
103 VkPtr<PFN_vkDestroyInstance> fDestroyInstance;
104 VkPtr<PFN_vkDeviceWaitIdle> fDeviceWaitIdle;
105 VkPtr<PFN_vkQueueWaitIdle> fQueueWaitIdle;
106 VkPtr<PFN_vkDestroyDevice> fDestroyDevice;
Greg Daniel35970ec2017-11-10 10:03:05 -0500107 VkPtr<PFN_vkGetDeviceQueue> fGetDeviceQueue;
jvanverthb0d43522016-04-21 11:46:23 -0700108
Greg Danielf730c182018-07-02 20:15:37 +0000109 sk_sp<const GrVkInterface> fInterface;
110
jvanverth9f372462016-04-06 06:08:59 -0700111 VkSurfaceKHR fSurface;
112 VkSwapchainKHR fSwapchain;
Greg Danielf730c182018-07-02 20:15:37 +0000113 uint32_t fGraphicsQueueIndex;
114 VkQueue fGraphicsQueue;
jvanverth9f372462016-04-06 06:08:59 -0700115 uint32_t fPresentQueueIndex;
116 VkQueue fPresentQueue;
jvanverth9f372462016-04-06 06:08:59 -0700117
jvanverthaf236b52016-05-20 06:01:06 -0700118 uint32_t fImageCount;
119 VkImage* fImages; // images in the swapchain
120 VkImageLayout* fImageLayouts; // layouts of these images when not color attachment
jvanverthaf236b52016-05-20 06:01:06 -0700121 sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts)
122 VkCommandPool fCommandPool;
123 BackbufferInfo* fBackbuffers;
124 uint32_t fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700125};
126
jvanvertha8d0d6c2016-05-05 12:32:03 -0700127} // namespace sk_app
128
jvanverth9f372462016-04-06 06:08:59 -0700129#endif // SK_VULKAN
130
131#endif