1 /*
2  * Copyright © 2017 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #ifndef WSI_COMMON_PRIVATE_H
24 #define WSI_COMMON_PRIVATE_H
25 
26 #include "wsi_common.h"
27 #include "vulkan/util/vk_object.h"
28 
29 struct wsi_image {
30    VkImage image;
31    VkDeviceMemory memory;
32 
33    struct {
34       VkBuffer buffer;
35       VkDeviceMemory memory;
36       VkCommandBuffer *blit_cmd_buffers;
37    } prime;
38 
39    uint64_t drm_modifier;
40    int num_planes;
41    uint32_t sizes[4];
42    uint32_t offsets[4];
43    uint32_t row_pitches[4];
44    int fds[4];
45 };
46 
47 struct wsi_swapchain {
48    struct vk_object_base base;
49 
50    const struct wsi_device *wsi;
51 
52    VkDevice device;
53    VkAllocationCallbacks alloc;
54    VkFence* fences;
55    VkPresentModeKHR present_mode;
56    uint32_t image_count;
57 
58    bool use_prime_blit;
59 
60    /* Command pools, one per queue family */
61    VkCommandPool *cmd_pools;
62 
63    VkResult (*destroy)(struct wsi_swapchain *swapchain,
64                        const VkAllocationCallbacks *pAllocator);
65    struct wsi_image *(*get_wsi_image)(struct wsi_swapchain *swapchain,
66                                       uint32_t image_index);
67    VkResult (*acquire_next_image)(struct wsi_swapchain *swap_chain,
68                                   const VkAcquireNextImageInfoKHR *info,
69                                   uint32_t *image_index);
70    VkResult (*queue_present)(struct wsi_swapchain *swap_chain,
71                              uint32_t image_index,
72                              const VkPresentRegionKHR *damage);
73 };
74 
75 bool
76 wsi_device_matches_drm_fd(const struct wsi_device *wsi, int drm_fd);
77 
78 VkResult
79 wsi_swapchain_init(const struct wsi_device *wsi,
80                    struct wsi_swapchain *chain,
81                    VkDevice device,
82                    const VkSwapchainCreateInfoKHR *pCreateInfo,
83                    const VkAllocationCallbacks *pAllocator);
84 
85 enum VkPresentModeKHR
86 wsi_swapchain_get_present_mode(struct wsi_device *wsi,
87                                const VkSwapchainCreateInfoKHR *pCreateInfo);
88 
89 void wsi_swapchain_finish(struct wsi_swapchain *chain);
90 
91 VkResult
92 wsi_create_native_image(const struct wsi_swapchain *chain,
93                         const VkSwapchainCreateInfoKHR *pCreateInfo,
94                         uint32_t num_modifier_lists,
95                         const uint32_t *num_modifiers,
96                         const uint64_t *const *modifiers,
97                         struct wsi_image *image);
98 
99 VkResult
100 wsi_create_prime_image(const struct wsi_swapchain *chain,
101                        const VkSwapchainCreateInfoKHR *pCreateInfo,
102                        bool use_modifier,
103                        struct wsi_image *image);
104 
105 void
106 wsi_destroy_image(const struct wsi_swapchain *chain,
107                   struct wsi_image *image);
108 
109 
110 struct wsi_interface {
111    VkResult (*get_support)(VkIcdSurfaceBase *surface,
112                            struct wsi_device *wsi_device,
113                            uint32_t queueFamilyIndex,
114                            VkBool32* pSupported);
115    VkResult (*get_capabilities2)(VkIcdSurfaceBase *surface,
116                                  struct wsi_device *wsi_device,
117                                  const void *info_next,
118                                  VkSurfaceCapabilities2KHR* pSurfaceCapabilities);
119    VkResult (*get_formats)(VkIcdSurfaceBase *surface,
120                            struct wsi_device *wsi_device,
121                            uint32_t* pSurfaceFormatCount,
122                            VkSurfaceFormatKHR* pSurfaceFormats);
123    VkResult (*get_formats2)(VkIcdSurfaceBase *surface,
124                             struct wsi_device *wsi_device,
125                             const void *info_next,
126                             uint32_t* pSurfaceFormatCount,
127                             VkSurfaceFormat2KHR* pSurfaceFormats);
128    VkResult (*get_present_modes)(VkIcdSurfaceBase *surface,
129                                  uint32_t* pPresentModeCount,
130                                  VkPresentModeKHR* pPresentModes);
131    VkResult (*get_present_rectangles)(VkIcdSurfaceBase *surface,
132                                       struct wsi_device *wsi_device,
133                                       uint32_t* pRectCount,
134                                       VkRect2D* pRects);
135    VkResult (*create_swapchain)(VkIcdSurfaceBase *surface,
136                                 VkDevice device,
137                                 struct wsi_device *wsi_device,
138                                 const VkSwapchainCreateInfoKHR* pCreateInfo,
139                                 const VkAllocationCallbacks* pAllocator,
140                                 struct wsi_swapchain **swapchain);
141 };
142 
143 VkResult wsi_x11_init_wsi(struct wsi_device *wsi_device,
144                           const VkAllocationCallbacks *alloc,
145                           const struct driOptionCache *dri_options);
146 void wsi_x11_finish_wsi(struct wsi_device *wsi_device,
147                         const VkAllocationCallbacks *alloc);
148 VkResult wsi_wl_init_wsi(struct wsi_device *wsi_device,
149                          const VkAllocationCallbacks *alloc,
150                          VkPhysicalDevice physical_device);
151 void wsi_wl_finish_wsi(struct wsi_device *wsi_device,
152                        const VkAllocationCallbacks *alloc);
153 
154 
155 VkResult
156 wsi_display_init_wsi(struct wsi_device *wsi_device,
157                      const VkAllocationCallbacks *alloc,
158                      int display_fd);
159 
160 void
161 wsi_display_finish_wsi(struct wsi_device *wsi_device,
162                        const VkAllocationCallbacks *alloc);
163 
164 VK_DEFINE_NONDISP_HANDLE_CASTS(wsi_swapchain, base, VkSwapchainKHR,
165                                VK_OBJECT_TYPE_SWAPCHAIN_KHR)
166 
167 #endif /* WSI_COMMON_PRIVATE_H */
168