blob: d40adf6e6ee09ece11997338b59d45d067b3e781 [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
jvanvertha8d0d6c2016-05-05 12:32:03 -070031 void resize(uint32_t w, uint32_t 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 {
brianosman05de2162016-05-06 13:28:57 -070036 this->createSwapchain(fWidth, fHeight, params);
jvanverth9f372462016-04-06 06:08:59 -070037 }
38
jvanvertha8d0d6c2016-05-05 12:32:03 -070039 GrBackendContext getBackendContext() override {
40 return (GrBackendContext) fBackendContext.get();
41 }
jvanverth9f372462016-04-06 06:08:59 -070042
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. */
46 using CanPresentFn = GrVkBackendContext::CanPresentFn;
47
48 VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn);
49
jvanverth9f372462016-04-06 06:08:59 -070050private:
jvanverth9f372462016-04-06 06:08:59 -070051 void destroyContext();
52
53 struct BackbufferInfo {
54 uint32_t fImageIndex; // image this is associated with
55 VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image
56 VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done
57 VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render
58 VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU
59 };
60
61 BackbufferInfo* getAvailableBackbuffer();
brianosman05de2162016-05-06 13:28:57 -070062 bool createSwapchain(uint32_t width, uint32_t height, const DisplayParams& params);
egdaniel58a8d922016-04-21 08:03:10 -070063 void createBuffers(VkFormat format);
jvanverth9f372462016-04-06 06:08:59 -070064 void destroyBuffers();
65
66 SkAutoTUnref<const GrVkBackendContext> fBackendContext;
67
jvanverthb0d43522016-04-21 11:46:23 -070068 // simple wrapper class that exists only to initialize a pointer to NULL
69 template <typename FNPTR_TYPE> class VkPtr {
70 public:
71 VkPtr() : fPtr(NULL) {}
72 VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
73 operator FNPTR_TYPE() const { return fPtr; }
74 private:
75 FNPTR_TYPE fPtr;
76 };
jvanvertha8d0d6c2016-05-05 12:32:03 -070077
jvanverthb0d43522016-04-21 11:46:23 -070078 // WSI interface functions
79 VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR;
80 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR;
81 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR;
82 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR;
83 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR;
84
85 VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR;
86 VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR;
87 VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR;
88 VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR;
89 VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR;
90 VkPtr<PFN_vkCreateSharedSwapchainsKHR> fCreateSharedSwapchainsKHR;
91
jvanverth9f372462016-04-06 06:08:59 -070092 VkSurfaceKHR fSurface;
93 VkSwapchainKHR fSwapchain;
94 uint32_t fPresentQueueIndex;
95 VkQueue fPresentQueue;
jvanverth9f372462016-04-06 06:08:59 -070096
jvanverthaf236b52016-05-20 06:01:06 -070097 uint32_t fImageCount;
98 VkImage* fImages; // images in the swapchain
99 VkImageLayout* fImageLayouts; // layouts of these images when not color attachment
100 sk_sp<GrRenderTarget>* fRenderTargets; // wrapped rendertargets for those images
101 sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts)
102 VkCommandPool fCommandPool;
103 BackbufferInfo* fBackbuffers;
104 uint32_t fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700105};
106
jvanvertha8d0d6c2016-05-05 12:32:03 -0700107} // namespace sk_app
108
jvanverth9f372462016-04-06 06:08:59 -0700109#endif // SK_VULKAN
110
111#endif