blob: 81e5f3d2d5b975f166e7769819c245d37586aaa5 [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
50 VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn);
51
jvanverth9f372462016-04-06 06:08:59 -070052private:
Jim Van Verthfbdc0802017-05-02 16:15:53 -040053 void initializeContext();
jvanverth9f372462016-04-06 06:08:59 -070054 void destroyContext();
55
56 struct BackbufferInfo {
57 uint32_t fImageIndex; // image this is associated with
58 VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image
59 VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done
60 VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render
61 VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU
62 };
63
64 BackbufferInfo* getAvailableBackbuffer();
bsalomonccde4ab2016-07-27 08:50:12 -070065 bool createSwapchain(int width, int height, const DisplayParams& params);
egdaniel58a8d922016-04-21 08:03:10 -070066 void createBuffers(VkFormat format);
jvanverth9f372462016-04-06 06:08:59 -070067 void destroyBuffers();
68
Hal Canary1b612a82016-11-03 16:26:13 -040069 sk_sp<const GrVkBackendContext> fBackendContext;
jvanverth9f372462016-04-06 06:08:59 -070070
jvanverthb0d43522016-04-21 11:46:23 -070071 // simple wrapper class that exists only to initialize a pointer to NULL
72 template <typename FNPTR_TYPE> class VkPtr {
73 public:
74 VkPtr() : fPtr(NULL) {}
75 VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
76 operator FNPTR_TYPE() const { return fPtr; }
77 private:
78 FNPTR_TYPE fPtr;
79 };
jvanvertha8d0d6c2016-05-05 12:32:03 -070080
Jim Van Verthfbdc0802017-05-02 16:15:53 -040081 // Create functions
82 CreateVkSurfaceFn fCreateVkSurfaceFn;
83 CanPresentFn fCanPresentFn;
84
jvanverthb0d43522016-04-21 11:46:23 -070085 // WSI interface functions
86 VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR;
87 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR;
88 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR;
89 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR;
90 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR;
91
92 VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR;
93 VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR;
94 VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR;
95 VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR;
96 VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR;
97 VkPtr<PFN_vkCreateSharedSwapchainsKHR> fCreateSharedSwapchainsKHR;
98
jvanverth9f372462016-04-06 06:08:59 -070099 VkSurfaceKHR fSurface;
100 VkSwapchainKHR fSwapchain;
101 uint32_t fPresentQueueIndex;
102 VkQueue fPresentQueue;
jvanverth9f372462016-04-06 06:08:59 -0700103
jvanverthaf236b52016-05-20 06:01:06 -0700104 uint32_t fImageCount;
105 VkImage* fImages; // images in the swapchain
106 VkImageLayout* fImageLayouts; // layouts of these images when not color attachment
jvanverthaf236b52016-05-20 06:01:06 -0700107 sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts)
108 VkCommandPool fCommandPool;
109 BackbufferInfo* fBackbuffers;
110 uint32_t fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700111};
112
jvanvertha8d0d6c2016-05-05 12:32:03 -0700113} // namespace sk_app
114
jvanverth9f372462016-04-06 06:08:59 -0700115#endif // SK_VULKAN
116
117#endif