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