• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkTypes.h" // required to pull in any SkUserConfig defines
12 
13 #ifdef SK_VULKAN
14 
15 #include "vk/GrVkBackendContext.h"
16 #include "WindowContext.h"
17 
18 class GrRenderTarget;
19 
20 namespace sk_app {
21 
22 class VulkanWindowContext : public WindowContext {
23 public:
24     ~VulkanWindowContext() override;
25 
26     sk_sp<SkSurface> getBackbufferSurface() override;
27     void swapBuffers() override;
28 
isValid()29     bool isValid() override { return SkToBool(fBackendContext.get()); }
30 
resize(int w,int h)31     void resize(int w, int h) override {
32         this->createSwapchain(w, h, fDisplayParams);
33     }
34 
setDisplayParams(const DisplayParams & params)35     void setDisplayParams(const DisplayParams& params) override {
36         this->destroyContext();
37         fDisplayParams = params;
38         this->initializeContext();
39     }
40 
getBackendContext()41     GrBackendContext getBackendContext() override {
42         return (GrBackendContext) fBackendContext.get();
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 = GrVkBackendContext::CanPresentFn;
49 
50     VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn);
51 
52 private:
53     void initializeContext();
54     void destroyContext();
55 
56     struct BackbufferInfo {
57         uint32_t        fImageIndex;          // image this is associated with
58         VkSemaphore     fAcquireSemaphore;    // we signal on this for acquisition of image
59         VkSemaphore     fRenderSemaphore;     // we wait on this for rendering to be done
60         VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render
61         VkFence         fUsageFences[2];      // used to ensure this data is no longer used on GPU
62     };
63 
64     BackbufferInfo* getAvailableBackbuffer();
65     bool createSwapchain(int width, int height, const DisplayParams& params);
66     void createBuffers(VkFormat format);
67     void destroyBuffers();
68 
69     sk_sp<const GrVkBackendContext> fBackendContext;
70 
71     // simple wrapper class that exists only to initialize a pointer to NULL
72     template <typename FNPTR_TYPE> class VkPtr {
73     public:
VkPtr()74         VkPtr() : fPtr(NULL) {}
75         VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
FNPTR_TYPE()76         operator FNPTR_TYPE() const { return fPtr; }
77     private:
78         FNPTR_TYPE fPtr;
79     };
80 
81     // Create functions
82     CreateVkSurfaceFn fCreateVkSurfaceFn;
83     CanPresentFn      fCanPresentFn;
84 
85     // WSI interface functions
86     VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR;
87     VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR;
88     VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR;
89     VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR;
90     VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR;
91 
92     VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR;
93     VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR;
94     VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR;
95     VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR;
96     VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR;
97     VkPtr<PFN_vkCreateSharedSwapchainsKHR> fCreateSharedSwapchainsKHR;
98 
99     VkSurfaceKHR      fSurface;
100     VkSwapchainKHR    fSwapchain;
101     uint32_t          fPresentQueueIndex;
102     VkQueue           fPresentQueue;
103 
104     uint32_t               fImageCount;
105     VkImage*               fImages;         // images in the swapchain
106     VkImageLayout*         fImageLayouts;   // layouts of these images when not color attachment
107     sk_sp<SkSurface>*      fSurfaces;       // surfaces client renders to (may not be based on rts)
108     VkCommandPool          fCommandPool;
109     BackbufferInfo*        fBackbuffers;
110     uint32_t               fCurrentBackbufferIndex;
111 };
112 
113 }   // namespace sk_app
114 
115 #endif // SK_VULKAN
116 
117 #endif
118