blob: 580dba273334550d0271637219145676feecdc47 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
djsollen9207cae2016-06-10 07:50:00 -070012
jvanverth9f372462016-04-06 06:08:59 -070013#ifdef SK_VULKAN
14
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/gpu/vk/GrVkVulkan.h"
Greg Daniel54bfb182018-11-20 17:12:36 -050016
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/gpu/vk/GrVkBackendContext.h"
18#include "src/gpu/vk/GrVkInterface.h"
19#include "tools/gpu/vk/VkTestUtils.h"
20#include "tools/sk_app/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
jvanverth9f372462016-04-06 06:08:59 -070059 VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done
jvanverth9f372462016-04-06 06:08:59 -070060 };
61
62 BackbufferInfo* getAvailableBackbuffer();
bsalomonccde4ab2016-07-27 08:50:12 -070063 bool createSwapchain(int width, int height, const DisplayParams& params);
Brian Salomone4e0aad2020-10-09 17:11:29 -040064 bool createBuffers(VkFormat format, VkImageUsageFlags, SkColorType colorType, VkSharingMode);
jvanverth9f372462016-04-06 06:08:59 -070065 void destroyBuffers();
66
Greg Danielf730c182018-07-02 20:15:37 +000067 VkInstance fInstance = VK_NULL_HANDLE;
68 VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE;
69 VkDevice fDevice = VK_NULL_HANDLE;
Greg Daniel37329b32018-07-02 20:16:44 +000070 VkDebugReportCallbackEXT fDebugCallback = VK_NULL_HANDLE;
jvanverth9f372462016-04-06 06:08:59 -070071
Jim Van Verthfbdc0802017-05-02 16:15:53 -040072 // Create functions
73 CreateVkSurfaceFn fCreateVkSurfaceFn;
74 CanPresentFn fCanPresentFn;
75
Greg Daniel35970ec2017-11-10 10:03:05 -050076 // Vulkan GetProcAddr functions
Greg Danielc8cd45a2018-07-12 10:02:37 -040077 PFN_vkGetInstanceProcAddr fGetInstanceProcAddr = nullptr;
78 PFN_vkGetDeviceProcAddr fGetDeviceProcAddr = nullptr;
Greg Daniel35970ec2017-11-10 10:03:05 -050079
jvanverthb0d43522016-04-21 11:46:23 -070080 // WSI interface functions
Greg Danielc8cd45a2018-07-12 10:02:37 -040081 PFN_vkDestroySurfaceKHR fDestroySurfaceKHR = nullptr;
82 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fGetPhysicalDeviceSurfaceSupportKHR = nullptr;
83 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fGetPhysicalDeviceSurfaceCapabilitiesKHR =nullptr;
84 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fGetPhysicalDeviceSurfaceFormatsKHR = nullptr;
85 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fGetPhysicalDeviceSurfacePresentModesKHR =nullptr;
jvanverthb0d43522016-04-21 11:46:23 -070086
Greg Danielc8cd45a2018-07-12 10:02:37 -040087 PFN_vkCreateSwapchainKHR fCreateSwapchainKHR = nullptr;
88 PFN_vkDestroySwapchainKHR fDestroySwapchainKHR = nullptr;
89 PFN_vkGetSwapchainImagesKHR fGetSwapchainImagesKHR = nullptr;
90 PFN_vkAcquireNextImageKHR fAcquireNextImageKHR = nullptr;
91 PFN_vkQueuePresentKHR fQueuePresentKHR = nullptr;
Greg Danielf730c182018-07-02 20:15:37 +000092
Greg Danielc8cd45a2018-07-12 10:02:37 -040093 PFN_vkDestroyInstance fDestroyInstance = nullptr;
94 PFN_vkDeviceWaitIdle fDeviceWaitIdle = nullptr;
Greg Daniela31f4e52018-08-01 16:48:52 -040095 PFN_vkDestroyDebugReportCallbackEXT fDestroyDebugReportCallbackEXT = nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040096 PFN_vkQueueWaitIdle fQueueWaitIdle = nullptr;
97 PFN_vkDestroyDevice fDestroyDevice = nullptr;
98 PFN_vkGetDeviceQueue fGetDeviceQueue = nullptr;
jvanverthb0d43522016-04-21 11:46:23 -070099
Greg Danielf730c182018-07-02 20:15:37 +0000100 sk_sp<const GrVkInterface> fInterface;
101
jvanverth9f372462016-04-06 06:08:59 -0700102 VkSurfaceKHR fSurface;
103 VkSwapchainKHR fSwapchain;
Greg Danielf730c182018-07-02 20:15:37 +0000104 uint32_t fGraphicsQueueIndex;
105 VkQueue fGraphicsQueue;
jvanverth9f372462016-04-06 06:08:59 -0700106 uint32_t fPresentQueueIndex;
107 VkQueue fPresentQueue;
jvanverth9f372462016-04-06 06:08:59 -0700108
jvanverthaf236b52016-05-20 06:01:06 -0700109 uint32_t fImageCount;
110 VkImage* fImages; // images in the swapchain
111 VkImageLayout* fImageLayouts; // layouts of these images when not color attachment
jvanverthaf236b52016-05-20 06:01:06 -0700112 sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts)
jvanverthaf236b52016-05-20 06:01:06 -0700113 BackbufferInfo* fBackbuffers;
114 uint32_t fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700115};
116
jvanvertha8d0d6c2016-05-05 12:32:03 -0700117} // namespace sk_app
118
jvanverth9f372462016-04-06 06:08:59 -0700119#endif // SK_VULKAN
120
121#endif