blob: 48f8ed536ba12a183c786abbd003678c2cb06141 [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
11#ifdef SK_VULKAN
12
jvanverth9f372462016-04-06 06:08:59 -070013#include "vk/GrVkBackendContext.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070014#include "WindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070015
16class SkSurface;
17class GrContext;
18
jvanvertha8d0d6c2016-05-05 12:32:03 -070019namespace sk_app {
20
21class VulkanWindowContext : public WindowContext {
jvanverth9f372462016-04-06 06:08:59 -070022public:
jvanvertha8d0d6c2016-05-05 12:32:03 -070023 ~VulkanWindowContext() override;
jvanverth9f372462016-04-06 06:08:59 -070024
25 // each platform will have to implement these in its CPP file
jvanverthb0d43522016-04-21 11:46:23 -070026 static VkSurfaceKHR createVkSurface(VkInstance, void* platformData);
27 static bool canPresent(VkInstance, VkPhysicalDevice, uint32_t queueFamilyIndex);
jvanverth9f372462016-04-06 06:08:59 -070028
jvanvertha8d0d6c2016-05-05 12:32:03 -070029 static VulkanWindowContext* Create(void* platformData, int msaaSampleCount) {
30 VulkanWindowContext* ctx = new VulkanWindowContext(platformData, msaaSampleCount);
jvanverth9f372462016-04-06 06:08:59 -070031 if (!ctx->isValid()) {
32 delete ctx;
33 return nullptr;
34 }
35 return ctx;
36 }
37
jvanvertha8d0d6c2016-05-05 12:32:03 -070038 SkSurface* getBackbufferSurface() override;
39 void swapBuffers() override;
jvanverth9f372462016-04-06 06:08:59 -070040
jvanvertha8d0d6c2016-05-05 12:32:03 -070041 bool makeCurrent() override { return true; }
jvanverth9f372462016-04-06 06:08:59 -070042
jvanvertha8d0d6c2016-05-05 12:32:03 -070043 bool isValid() override { return SkToBool(fBackendContext.get()); }
jvanverth9f372462016-04-06 06:08:59 -070044
jvanvertha8d0d6c2016-05-05 12:32:03 -070045 void resize(uint32_t w, uint32_t h) override {
46 this->createSwapchain(w, h);
jvanverth9f372462016-04-06 06:08:59 -070047 }
48
jvanvertha8d0d6c2016-05-05 12:32:03 -070049 GrBackendContext getBackendContext() override {
50 return (GrBackendContext) fBackendContext.get();
51 }
jvanverth9f372462016-04-06 06:08:59 -070052
53private:
jvanvertha8d0d6c2016-05-05 12:32:03 -070054 VulkanWindowContext();
55 VulkanWindowContext(void*, int msaaSampleCount);
jvanverth9f372462016-04-06 06:08:59 -070056 void initializeContext(void*);
57 void destroyContext();
58
59 struct BackbufferInfo {
60 uint32_t fImageIndex; // image this is associated with
61 VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image
62 VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done
63 VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render
64 VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU
65 };
66
67 BackbufferInfo* getAvailableBackbuffer();
68 bool createSwapchain(uint32_t width, uint32_t height);
egdaniel58a8d922016-04-21 08:03:10 -070069 void createBuffers(VkFormat format);
jvanverth9f372462016-04-06 06:08:59 -070070 void destroyBuffers();
71
72 SkAutoTUnref<const GrVkBackendContext> fBackendContext;
73
jvanverthb0d43522016-04-21 11:46:23 -070074 // simple wrapper class that exists only to initialize a pointer to NULL
75 template <typename FNPTR_TYPE> class VkPtr {
76 public:
77 VkPtr() : fPtr(NULL) {}
78 VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
79 operator FNPTR_TYPE() const { return fPtr; }
80 private:
81 FNPTR_TYPE fPtr;
82 };
jvanvertha8d0d6c2016-05-05 12:32:03 -070083
jvanverthb0d43522016-04-21 11:46:23 -070084 // WSI interface functions
85 VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR;
86 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR;
87 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR;
88 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR;
89 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR;
90
91 VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR;
92 VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR;
93 VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR;
94 VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR;
95 VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR;
96 VkPtr<PFN_vkCreateSharedSwapchainsKHR> fCreateSharedSwapchainsKHR;
97
jvanverth9f372462016-04-06 06:08:59 -070098 GrContext* fContext;
99 VkSurfaceKHR fSurface;
100 VkSwapchainKHR fSwapchain;
101 uint32_t fPresentQueueIndex;
102 VkQueue fPresentQueue;
103 int fWidth;
104 int fHeight;
105 GrPixelConfig fPixelConfig;
106
107 uint32_t fImageCount;
108 VkImage* fImages; // images in the swapchain
109 VkImageLayout* fImageLayouts; // layouts of these images when not color attachment
110 sk_sp<SkSurface>* fSurfaces; // wrapped surface for those images
111 VkCommandPool fCommandPool;
112 BackbufferInfo* fBackbuffers;
113 uint32_t fCurrentBackbufferIndex;
114};
115
jvanvertha8d0d6c2016-05-05 12:32:03 -0700116} // namespace sk_app
117
jvanverth9f372462016-04-06 06:08:59 -0700118#endif // SK_VULKAN
119
120#endif