blob: 1fbf18db0fbdbc9546dfdfa5c075adacd86b5aaf [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
Greg Daniel54bfb182018-11-20 17:12:36 -050015#include "vk/GrVkVulkan.h"
16
jvanverth9f372462016-04-06 06:08:59 -070017#include "vk/GrVkBackendContext.h"
Greg Danielf730c182018-07-02 20:15:37 +000018#include "vk/GrVkInterface.h"
19#include "vk/VkTestUtils.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070020#include "WindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070021
jvanverthaf236b52016-05-20 06:01:06 -070022class GrRenderTarget;
jvanverth9f372462016-04-06 06:08:59 -070023
jvanvertha8d0d6c2016-05-05 12:32:03 -070024namespace sk_app {
25
26class VulkanWindowContext : public WindowContext {
jvanverth9f372462016-04-06 06:08:59 -070027public:
jvanvertha8d0d6c2016-05-05 12:32:03 -070028 ~VulkanWindowContext() override;
jvanverth9f372462016-04-06 06:08:59 -070029
jvanverthaf236b52016-05-20 06:01:06 -070030 sk_sp<SkSurface> getBackbufferSurface() override;
jvanvertha8d0d6c2016-05-05 12:32:03 -070031 void swapBuffers() override;
jvanverth9f372462016-04-06 06:08:59 -070032
Greg Danielf730c182018-07-02 20:15:37 +000033 bool isValid() override { return fDevice != VK_NULL_HANDLE; }
jvanverth9f372462016-04-06 06:08:59 -070034
bsalomonccde4ab2016-07-27 08:50:12 -070035 void resize(int w, int h) override {
brianosman05de2162016-05-06 13:28:57 -070036 this->createSwapchain(w, h, fDisplayParams);
37 }
38
liyuqian796c5bb2016-05-09 08:49:29 -070039 void setDisplayParams(const DisplayParams& params) override {
Jim Van Verthfbdc0802017-05-02 16:15:53 -040040 this->destroyContext();
41 fDisplayParams = params;
42 this->initializeContext();
jvanverth9f372462016-04-06 06:08:59 -070043 }
44
bsalomond1bdd1f2016-07-26 12:02:50 -070045 /** Platform specific function that creates a VkSurfaceKHR for a window */
46 using CreateVkSurfaceFn = std::function<VkSurfaceKHR(VkInstance)>;
47 /** Platform specific function that determines whether presentation will succeed. */
Greg Danielf730c182018-07-02 20:15:37 +000048 using CanPresentFn = sk_gpu_test::CanPresentFn;
bsalomond1bdd1f2016-07-26 12:02:50 -070049
Greg Daniel35970ec2017-11-10 10:03:05 -050050 VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn,
51 PFN_vkGetInstanceProcAddr, PFN_vkGetDeviceProcAddr);
bsalomond1bdd1f2016-07-26 12:02:50 -070052
jvanverth9f372462016-04-06 06:08:59 -070053private:
Jim Van Verthfbdc0802017-05-02 16:15:53 -040054 void initializeContext();
jvanverth9f372462016-04-06 06:08:59 -070055 void destroyContext();
56
57 struct BackbufferInfo {
58 uint32_t fImageIndex; // image this is associated with
59 VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image
60 VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done
61 VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render
62 VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU
63 };
64
65 BackbufferInfo* getAvailableBackbuffer();
bsalomonccde4ab2016-07-27 08:50:12 -070066 bool createSwapchain(int width, int height, const DisplayParams& params);
Greg Danielfaa095e2017-12-19 13:15:02 -050067 void createBuffers(VkFormat format, SkColorType colorType);
jvanverth9f372462016-04-06 06:08:59 -070068 void destroyBuffers();
69
Greg Danielf730c182018-07-02 20:15:37 +000070 VkInstance fInstance = VK_NULL_HANDLE;
71 VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE;
72 VkDevice fDevice = VK_NULL_HANDLE;
Greg Daniel37329b32018-07-02 20:16:44 +000073 VkDebugReportCallbackEXT fDebugCallback = VK_NULL_HANDLE;
jvanverth9f372462016-04-06 06:08:59 -070074
Jim Van Verthfbdc0802017-05-02 16:15:53 -040075 // Create functions
76 CreateVkSurfaceFn fCreateVkSurfaceFn;
77 CanPresentFn fCanPresentFn;
78
Greg Daniel35970ec2017-11-10 10:03:05 -050079 // Vulkan GetProcAddr functions
Greg Danielc8cd45a2018-07-12 10:02:37 -040080 PFN_vkGetInstanceProcAddr fGetInstanceProcAddr = nullptr;
81 PFN_vkGetDeviceProcAddr fGetDeviceProcAddr = nullptr;
Greg Daniel35970ec2017-11-10 10:03:05 -050082
jvanverthb0d43522016-04-21 11:46:23 -070083 // WSI interface functions
Greg Danielc8cd45a2018-07-12 10:02:37 -040084 PFN_vkDestroySurfaceKHR fDestroySurfaceKHR = nullptr;
85 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fGetPhysicalDeviceSurfaceSupportKHR = nullptr;
86 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fGetPhysicalDeviceSurfaceCapabilitiesKHR =nullptr;
87 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fGetPhysicalDeviceSurfaceFormatsKHR = nullptr;
88 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fGetPhysicalDeviceSurfacePresentModesKHR =nullptr;
jvanverthb0d43522016-04-21 11:46:23 -070089
Greg Danielc8cd45a2018-07-12 10:02:37 -040090 PFN_vkCreateSwapchainKHR fCreateSwapchainKHR = nullptr;
91 PFN_vkDestroySwapchainKHR fDestroySwapchainKHR = nullptr;
92 PFN_vkGetSwapchainImagesKHR fGetSwapchainImagesKHR = nullptr;
93 PFN_vkAcquireNextImageKHR fAcquireNextImageKHR = nullptr;
94 PFN_vkQueuePresentKHR fQueuePresentKHR = nullptr;
Greg Danielf730c182018-07-02 20:15:37 +000095
Greg Danielc8cd45a2018-07-12 10:02:37 -040096 PFN_vkDestroyInstance fDestroyInstance = nullptr;
97 PFN_vkDeviceWaitIdle fDeviceWaitIdle = nullptr;
Greg Daniela31f4e52018-08-01 16:48:52 -040098 PFN_vkDestroyDebugReportCallbackEXT fDestroyDebugReportCallbackEXT = nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040099 PFN_vkQueueWaitIdle fQueueWaitIdle = nullptr;
100 PFN_vkDestroyDevice fDestroyDevice = nullptr;
101 PFN_vkGetDeviceQueue fGetDeviceQueue = nullptr;
jvanverthb0d43522016-04-21 11:46:23 -0700102
Greg Danielf730c182018-07-02 20:15:37 +0000103 sk_sp<const GrVkInterface> fInterface;
104
jvanverth9f372462016-04-06 06:08:59 -0700105 VkSurfaceKHR fSurface;
106 VkSwapchainKHR fSwapchain;
Greg Danielf730c182018-07-02 20:15:37 +0000107 uint32_t fGraphicsQueueIndex;
108 VkQueue fGraphicsQueue;
jvanverth9f372462016-04-06 06:08:59 -0700109 uint32_t fPresentQueueIndex;
110 VkQueue fPresentQueue;
jvanverth9f372462016-04-06 06:08:59 -0700111
jvanverthaf236b52016-05-20 06:01:06 -0700112 uint32_t fImageCount;
113 VkImage* fImages; // images in the swapchain
114 VkImageLayout* fImageLayouts; // layouts of these images when not color attachment
jvanverthaf236b52016-05-20 06:01:06 -0700115 sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts)
116 VkCommandPool fCommandPool;
117 BackbufferInfo* fBackbuffers;
118 uint32_t fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700119};
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