blob: d02b11428e17d8f33b085dab68dc5899224ffd70 [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"
jvanvertha8d0d6c2016-05-05 12:32:03 -070016#include "WindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070017
jvanverthaf236b52016-05-20 06:01:06 -070018class GrRenderTarget;
jvanverth9f372462016-04-06 06:08:59 -070019
jvanvertha8d0d6c2016-05-05 12:32:03 -070020namespace sk_app {
21
22class VulkanWindowContext : public WindowContext {
jvanverth9f372462016-04-06 06:08:59 -070023public:
jvanvertha8d0d6c2016-05-05 12:32:03 -070024 ~VulkanWindowContext() override;
jvanverth9f372462016-04-06 06:08:59 -070025
jvanverthaf236b52016-05-20 06:01:06 -070026 sk_sp<SkSurface> getBackbufferSurface() override;
jvanvertha8d0d6c2016-05-05 12:32:03 -070027 void swapBuffers() override;
jvanverth9f372462016-04-06 06:08:59 -070028
jvanvertha8d0d6c2016-05-05 12:32:03 -070029 bool isValid() override { return SkToBool(fBackendContext.get()); }
jvanverth9f372462016-04-06 06:08:59 -070030
bsalomonccde4ab2016-07-27 08:50:12 -070031 void resize(int w, int h) override {
brianosman05de2162016-05-06 13:28:57 -070032 this->createSwapchain(w, h, fDisplayParams);
33 }
34
liyuqian796c5bb2016-05-09 08:49:29 -070035 void setDisplayParams(const DisplayParams& params) override {
Jim Van Verthfbdc0802017-05-02 16:15:53 -040036 this->destroyContext();
37 fDisplayParams = params;
38 this->initializeContext();
jvanverth9f372462016-04-06 06:08:59 -070039 }
40
Greg Daniel1f05f442016-10-27 16:37:17 -040041 GrBackendContext getBackendContext() override {
42 return (GrBackendContext) fBackendContext.get();
jvanvertha8d0d6c2016-05-05 12:32:03 -070043 }
jvanverth9f372462016-04-06 06:08:59 -070044
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. */
48 using CanPresentFn = GrVkBackendContext::CanPresentFn;
49
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);
egdaniel58a8d922016-04-21 08:03:10 -070067 void createBuffers(VkFormat format);
jvanverth9f372462016-04-06 06:08:59 -070068 void destroyBuffers();
69
Hal Canary1b612a82016-11-03 16:26:13 -040070 sk_sp<const GrVkBackendContext> fBackendContext;
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 Daniel35970ec2017-11-10 10:03:05 -0500102 VkPtr<PFN_vkGetDeviceQueue> fGetDeviceQueue;
jvanverthb0d43522016-04-21 11:46:23 -0700103
jvanverth9f372462016-04-06 06:08:59 -0700104 VkSurfaceKHR fSurface;
105 VkSwapchainKHR fSwapchain;
106 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)
113 VkCommandPool fCommandPool;
114 BackbufferInfo* fBackbuffers;
115 uint32_t fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700116};
117
jvanvertha8d0d6c2016-05-05 12:32:03 -0700118} // namespace sk_app
119
jvanverth9f372462016-04-06 06:08:59 -0700120#endif // SK_VULKAN
121
122#endif