1 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 */ 8 #ifndef VulkanWindowContext_DEFINED 9 #define VulkanWindowContext_DEFINED 10 11 #include "include/core/SkTypes.h" 12 13 #ifdef SK_VULKAN 14 15 #include "include/gpu/vk/GrVkBackendContext.h" 16 #include "tools/gpu/vk/VkTestUtils.h" 17 #include "tools/sk_app/WindowContext.h" 18 19 class GrRenderTarget; 20 21 namespace skgpu { struct VulkanInterface; } 22 23 namespace sk_app { 24 25 class VulkanWindowContext : public WindowContext { 26 public: 27 ~VulkanWindowContext() override; 28 29 sk_sp<SkSurface> getBackbufferSurface() override; 30 void swapBuffers() override; 31 isValid()32 bool isValid() override { return fDevice != VK_NULL_HANDLE; } 33 resize(int w,int h)34 void resize(int w, int h) override { 35 this->createSwapchain(w, h, fDisplayParams); 36 } 37 setDisplayParams(const DisplayParams & params)38 void setDisplayParams(const DisplayParams& params) override { 39 this->destroyContext(); 40 fDisplayParams = params; 41 this->initializeContext(); 42 } 43 44 /** Platform specific function that creates a VkSurfaceKHR for a window */ 45 using CreateVkSurfaceFn = std::function<VkSurfaceKHR(VkInstance)>; 46 /** Platform specific function that determines whether presentation will succeed. */ 47 using CanPresentFn = sk_gpu_test::CanPresentFn; 48 49 VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn, 50 PFN_vkGetInstanceProcAddr); 51 52 private: 53 void initializeContext(); 54 void destroyContext(); 55 56 struct BackbufferInfo { 57 uint32_t fImageIndex; // image this is associated with 58 VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done 59 }; 60 61 BackbufferInfo* getAvailableBackbuffer(); 62 bool createSwapchain(int width, int height, const DisplayParams& params); 63 bool createBuffers(VkFormat format, VkImageUsageFlags, SkColorType colorType, VkSharingMode); 64 void destroyBuffers(); 65 66 VkInstance fInstance = VK_NULL_HANDLE; 67 VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE; 68 VkDevice fDevice = VK_NULL_HANDLE; 69 VkDebugReportCallbackEXT fDebugCallback = VK_NULL_HANDLE; 70 71 // Create functions 72 CreateVkSurfaceFn fCreateVkSurfaceFn; 73 CanPresentFn fCanPresentFn; 74 75 PFN_vkGetInstanceProcAddr fGetInstanceProcAddr = nullptr; 76 77 // WSI interface functions 78 PFN_vkDestroySurfaceKHR fDestroySurfaceKHR = nullptr; 79 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fGetPhysicalDeviceSurfaceSupportKHR = nullptr; 80 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fGetPhysicalDeviceSurfaceCapabilitiesKHR =nullptr; 81 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fGetPhysicalDeviceSurfaceFormatsKHR = nullptr; 82 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fGetPhysicalDeviceSurfacePresentModesKHR =nullptr; 83 84 PFN_vkCreateSwapchainKHR fCreateSwapchainKHR = nullptr; 85 PFN_vkDestroySwapchainKHR fDestroySwapchainKHR = nullptr; 86 PFN_vkGetSwapchainImagesKHR fGetSwapchainImagesKHR = nullptr; 87 PFN_vkAcquireNextImageKHR fAcquireNextImageKHR = nullptr; 88 PFN_vkQueuePresentKHR fQueuePresentKHR = nullptr; 89 90 PFN_vkDestroyInstance fDestroyInstance = nullptr; 91 PFN_vkDeviceWaitIdle fDeviceWaitIdle = nullptr; 92 PFN_vkDestroyDebugReportCallbackEXT fDestroyDebugReportCallbackEXT = nullptr; 93 PFN_vkQueueWaitIdle fQueueWaitIdle = nullptr; 94 PFN_vkDestroyDevice fDestroyDevice = nullptr; 95 PFN_vkGetDeviceQueue fGetDeviceQueue = nullptr; 96 97 sk_sp<const skgpu::VulkanInterface> fInterface; 98 99 VkSurfaceKHR fSurface; 100 VkSwapchainKHR fSwapchain; 101 uint32_t fGraphicsQueueIndex; 102 VkQueue fGraphicsQueue; 103 uint32_t fPresentQueueIndex; 104 VkQueue fPresentQueue; 105 106 uint32_t fImageCount; 107 VkImage* fImages; // images in the swapchain 108 VkImageLayout* fImageLayouts; // layouts of these images when not color attachment 109 sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts) 110 BackbufferInfo* fBackbuffers; 111 uint32_t fCurrentBackbufferIndex; 112 }; 113 114 } // namespace sk_app 115 116 #endif // SK_VULKAN 117 118 #endif 119