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