VulkanWindowContext.h 4.2 KB

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