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/runtime/vk_object.h"
28#include "vulkan/runtime/vk_sync.h"
29
30struct wsi_image;
31struct wsi_swapchain;
32
33#define WSI_DEBUG_BUFFER      (1ull << 0)
34#define WSI_DEBUG_SW          (1ull << 1)
35#define WSI_DEBUG_NOSHM       (1ull << 2)
36#define WSI_DEBUG_LINEAR      (1ull << 3)
37
38extern uint64_t WSI_DEBUG;
39
40struct wsi_image_info {
41   VkImageCreateInfo create;
42   struct wsi_image_create_info wsi;
43   VkExternalMemoryImageCreateInfo ext_mem;
44   VkImageFormatListCreateInfo format_list;
45   VkImageDrmFormatModifierListCreateInfoEXT drm_mod_list;
46
47   bool prime_use_linear_modifier;
48
49   /* Not really part of VkImageCreateInfo but needed to figure out the
50    * number of planes we need to bind.
51    */
52   uint32_t modifier_prop_count;
53   struct VkDrmFormatModifierPropertiesEXT *modifier_props;
54
55   /* For buffer blit images, the linear stride in bytes */
56   uint32_t linear_stride;
57
58   /* For buffer blit images, the size of the buffer in bytes */
59   uint32_t linear_size;
60
61   uint32_t (*select_image_memory_type)(const struct wsi_device *wsi,
62                                        uint32_t type_bits);
63   uint32_t (*select_buffer_memory_type)(const struct wsi_device *wsi,
64                                         uint32_t type_bits);
65
66   uint8_t *(*alloc_shm)(struct wsi_image *image, unsigned size);
67
68   VkResult (*create_mem)(const struct wsi_swapchain *chain,
69                          const struct wsi_image_info *info,
70                          struct wsi_image *image);
71
72   VkResult (*finish_create)(const struct wsi_swapchain *chain,
73                             const struct wsi_image_info *info,
74                             struct wsi_image *image);
75};
76
77struct wsi_image {
78   VkImage image;
79   VkDeviceMemory memory;
80
81   struct {
82      VkBuffer buffer;
83      VkDeviceMemory memory;
84      VkCommandBuffer *blit_cmd_buffers;
85   } buffer;
86
87#ifndef _WIN32
88   uint64_t drm_modifier;
89#endif
90   int num_planes;
91   uint32_t sizes[4];
92   uint32_t offsets[4];
93   uint32_t row_pitches[4];
94#ifndef _WIN32
95   int dma_buf_fd;
96#endif
97   void *cpu_map;
98};
99
100struct wsi_swapchain {
101   struct vk_object_base base;
102
103   const struct wsi_device *wsi;
104
105   VkDevice device;
106   VkAllocationCallbacks alloc;
107   VkFence* fences;
108   VkSemaphore* buffer_blit_semaphores;
109   VkPresentModeKHR present_mode;
110
111   int signal_dma_buf_from_semaphore;
112   VkSemaphore dma_buf_semaphore;
113
114   struct wsi_image_info image_info;
115   uint32_t image_count;
116
117   bool use_buffer_blit;
118
119   /* If the driver wants to use a special queue to execute the buffer blit,
120    * it'll implement the wsi_device::get_buffer_blit_queue callback.
121    * The created queue will be stored here and will be used to execute the
122    * buffer blit instead of using the present queue.
123    */
124   VkQueue buffer_blit_queue;
125
126   /* Command pools, one per queue family */
127   VkCommandPool *cmd_pools;
128
129   VkResult (*destroy)(struct wsi_swapchain *swapchain,
130                       const VkAllocationCallbacks *pAllocator);
131   struct wsi_image *(*get_wsi_image)(struct wsi_swapchain *swapchain,
132                                      uint32_t image_index);
133   VkResult (*acquire_next_image)(struct wsi_swapchain *swap_chain,
134                                  const VkAcquireNextImageInfoKHR *info,
135                                  uint32_t *image_index);
136   VkResult (*queue_present)(struct wsi_swapchain *swap_chain,
137                             uint32_t image_index,
138                             const VkPresentRegionKHR *damage);
139};
140
141bool
142wsi_device_matches_drm_fd(const struct wsi_device *wsi, int drm_fd);
143
144VkResult
145wsi_swapchain_init(const struct wsi_device *wsi,
146                   struct wsi_swapchain *chain,
147                   VkDevice device,
148                   const VkSwapchainCreateInfoKHR *pCreateInfo,
149                   const VkAllocationCallbacks *pAllocator,
150                   bool use_buffer_blit);
151
152enum VkPresentModeKHR
153wsi_swapchain_get_present_mode(struct wsi_device *wsi,
154                               const VkSwapchainCreateInfoKHR *pCreateInfo);
155
156void wsi_swapchain_finish(struct wsi_swapchain *chain);
157
158uint32_t
159wsi_select_memory_type(const struct wsi_device *wsi,
160                       VkMemoryPropertyFlags req_flags,
161                       VkMemoryPropertyFlags deny_flags,
162                       uint32_t type_bits);
163uint32_t
164wsi_select_device_memory_type(const struct wsi_device *wsi,
165                              uint32_t type_bits);
166
167VkResult
168wsi_configure_native_image(const struct wsi_swapchain *chain,
169                           const VkSwapchainCreateInfoKHR *pCreateInfo,
170                           uint32_t num_modifier_lists,
171                           const uint32_t *num_modifiers,
172                           const uint64_t *const *modifiers,
173                           struct wsi_image_info *info);
174
175VkResult
176wsi_configure_prime_image(UNUSED const struct wsi_swapchain *chain,
177                          const VkSwapchainCreateInfoKHR *pCreateInfo,
178                          bool use_modifier,
179                          struct wsi_image_info *info);
180
181VkResult
182wsi_configure_cpu_image(const struct wsi_swapchain *chain,
183                        const VkSwapchainCreateInfoKHR *pCreateInfo,
184                        uint8_t *(alloc_shm)(struct wsi_image *image,
185                                             unsigned size),
186                        struct wsi_image_info *info);
187
188VkResult
189wsi_create_buffer_image_mem(const struct wsi_swapchain *chain,
190                            const struct wsi_image_info *info,
191                            struct wsi_image *image,
192                            VkExternalMemoryHandleTypeFlags handle_types,
193                            bool implicit_sync);
194
195VkResult
196wsi_finish_create_buffer_image(const struct wsi_swapchain *chain,
197                               const struct wsi_image_info *info,
198                               struct wsi_image *image);
199
200VkResult
201wsi_configure_buffer_image(UNUSED const struct wsi_swapchain *chain,
202                           const VkSwapchainCreateInfoKHR *pCreateInfo,
203                           uint32_t stride_align, uint32_t size_align,
204                           struct wsi_image_info *info);
205
206VkResult
207wsi_configure_image(const struct wsi_swapchain *chain,
208                    const VkSwapchainCreateInfoKHR *pCreateInfo,
209                    VkExternalMemoryHandleTypeFlags handle_types,
210                    struct wsi_image_info *info);
211void
212wsi_destroy_image_info(const struct wsi_swapchain *chain,
213                       struct wsi_image_info *info);
214VkResult
215wsi_create_image(const struct wsi_swapchain *chain,
216                 const struct wsi_image_info *info,
217                 struct wsi_image *image);
218void
219wsi_image_init(struct wsi_image *image);
220
221void
222wsi_destroy_image(const struct wsi_swapchain *chain,
223                  struct wsi_image *image);
224
225#ifdef HAVE_LIBDRM
226VkResult
227wsi_prepare_signal_dma_buf_from_semaphore(struct wsi_swapchain *chain,
228                                          const struct wsi_image *image);
229VkResult
230wsi_signal_dma_buf_from_semaphore(const struct wsi_swapchain *chain,
231                                  const struct wsi_image *image);
232VkResult
233wsi_create_sync_for_dma_buf_wait(const struct wsi_swapchain *chain,
234                                 const struct wsi_image *image,
235                                 enum vk_sync_features sync_features,
236                                 struct vk_sync **sync_out);
237#endif
238
239struct wsi_interface {
240   VkResult (*get_support)(VkIcdSurfaceBase *surface,
241                           struct wsi_device *wsi_device,
242                           uint32_t queueFamilyIndex,
243                           VkBool32* pSupported);
244   VkResult (*get_capabilities2)(VkIcdSurfaceBase *surface,
245                                 struct wsi_device *wsi_device,
246                                 const void *info_next,
247                                 VkSurfaceCapabilities2KHR* pSurfaceCapabilities);
248   VkResult (*get_formats)(VkIcdSurfaceBase *surface,
249                           struct wsi_device *wsi_device,
250                           uint32_t* pSurfaceFormatCount,
251                           VkSurfaceFormatKHR* pSurfaceFormats);
252   VkResult (*get_formats2)(VkIcdSurfaceBase *surface,
253                            struct wsi_device *wsi_device,
254                            const void *info_next,
255                            uint32_t* pSurfaceFormatCount,
256                            VkSurfaceFormat2KHR* pSurfaceFormats);
257   VkResult (*get_present_modes)(VkIcdSurfaceBase *surface,
258                                 uint32_t* pPresentModeCount,
259                                 VkPresentModeKHR* pPresentModes);
260   VkResult (*get_present_rectangles)(VkIcdSurfaceBase *surface,
261                                      struct wsi_device *wsi_device,
262                                      uint32_t* pRectCount,
263                                      VkRect2D* pRects);
264   VkResult (*create_swapchain)(VkIcdSurfaceBase *surface,
265                                VkDevice device,
266                                struct wsi_device *wsi_device,
267                                const VkSwapchainCreateInfoKHR* pCreateInfo,
268                                const VkAllocationCallbacks* pAllocator,
269                                struct wsi_swapchain **swapchain);
270};
271
272VkResult wsi_x11_init_wsi(struct wsi_device *wsi_device,
273                          const VkAllocationCallbacks *alloc,
274                          const struct driOptionCache *dri_options);
275void wsi_x11_finish_wsi(struct wsi_device *wsi_device,
276                        const VkAllocationCallbacks *alloc);
277VkResult wsi_wl_init_wsi(struct wsi_device *wsi_device,
278                         const VkAllocationCallbacks *alloc,
279                         VkPhysicalDevice physical_device);
280void wsi_wl_finish_wsi(struct wsi_device *wsi_device,
281                       const VkAllocationCallbacks *alloc);
282VkResult wsi_win32_init_wsi(struct wsi_device *wsi_device,
283                         const VkAllocationCallbacks *alloc,
284                         VkPhysicalDevice physical_device);
285void wsi_win32_finish_wsi(struct wsi_device *wsi_device,
286                       const VkAllocationCallbacks *alloc);
287
288
289VkResult
290wsi_display_init_wsi(struct wsi_device *wsi_device,
291                     const VkAllocationCallbacks *alloc,
292                     int display_fd);
293
294void
295wsi_display_finish_wsi(struct wsi_device *wsi_device,
296                       const VkAllocationCallbacks *alloc);
297
298void
299wsi_display_setup_syncobj_fd(struct wsi_device *wsi_device,
300                             int fd);
301
302VK_DEFINE_NONDISP_HANDLE_CASTS(wsi_swapchain, base, VkSwapchainKHR,
303                               VK_OBJECT_TYPE_SWAPCHAIN_KHR)
304
305#endif /* WSI_COMMON_PRIVATE_H */
306