1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci#ifndef WSI_COMMON_PRIVATE_H
24bf215546Sopenharmony_ci#define WSI_COMMON_PRIVATE_H
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "wsi_common.h"
27bf215546Sopenharmony_ci#include "vulkan/runtime/vk_object.h"
28bf215546Sopenharmony_ci#include "vulkan/runtime/vk_sync.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_cistruct wsi_image;
31bf215546Sopenharmony_cistruct wsi_swapchain;
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#define WSI_DEBUG_BUFFER      (1ull << 0)
34bf215546Sopenharmony_ci#define WSI_DEBUG_SW          (1ull << 1)
35bf215546Sopenharmony_ci#define WSI_DEBUG_NOSHM       (1ull << 2)
36bf215546Sopenharmony_ci#define WSI_DEBUG_LINEAR      (1ull << 3)
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ciextern uint64_t WSI_DEBUG;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistruct wsi_image_info {
41bf215546Sopenharmony_ci   VkImageCreateInfo create;
42bf215546Sopenharmony_ci   struct wsi_image_create_info wsi;
43bf215546Sopenharmony_ci   VkExternalMemoryImageCreateInfo ext_mem;
44bf215546Sopenharmony_ci   VkImageFormatListCreateInfo format_list;
45bf215546Sopenharmony_ci   VkImageDrmFormatModifierListCreateInfoEXT drm_mod_list;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   bool prime_use_linear_modifier;
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   /* Not really part of VkImageCreateInfo but needed to figure out the
50bf215546Sopenharmony_ci    * number of planes we need to bind.
51bf215546Sopenharmony_ci    */
52bf215546Sopenharmony_ci   uint32_t modifier_prop_count;
53bf215546Sopenharmony_ci   struct VkDrmFormatModifierPropertiesEXT *modifier_props;
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   /* For buffer blit images, the linear stride in bytes */
56bf215546Sopenharmony_ci   uint32_t linear_stride;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   /* For buffer blit images, the size of the buffer in bytes */
59bf215546Sopenharmony_ci   uint32_t linear_size;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   uint32_t (*select_image_memory_type)(const struct wsi_device *wsi,
62bf215546Sopenharmony_ci                                        uint32_t type_bits);
63bf215546Sopenharmony_ci   uint32_t (*select_buffer_memory_type)(const struct wsi_device *wsi,
64bf215546Sopenharmony_ci                                         uint32_t type_bits);
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   uint8_t *(*alloc_shm)(struct wsi_image *image, unsigned size);
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   VkResult (*create_mem)(const struct wsi_swapchain *chain,
69bf215546Sopenharmony_ci                          const struct wsi_image_info *info,
70bf215546Sopenharmony_ci                          struct wsi_image *image);
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   VkResult (*finish_create)(const struct wsi_swapchain *chain,
73bf215546Sopenharmony_ci                             const struct wsi_image_info *info,
74bf215546Sopenharmony_ci                             struct wsi_image *image);
75bf215546Sopenharmony_ci};
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cistruct wsi_image {
78bf215546Sopenharmony_ci   VkImage image;
79bf215546Sopenharmony_ci   VkDeviceMemory memory;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   struct {
82bf215546Sopenharmony_ci      VkBuffer buffer;
83bf215546Sopenharmony_ci      VkDeviceMemory memory;
84bf215546Sopenharmony_ci      VkCommandBuffer *blit_cmd_buffers;
85bf215546Sopenharmony_ci   } buffer;
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci#ifndef _WIN32
88bf215546Sopenharmony_ci   uint64_t drm_modifier;
89bf215546Sopenharmony_ci#endif
90bf215546Sopenharmony_ci   int num_planes;
91bf215546Sopenharmony_ci   uint32_t sizes[4];
92bf215546Sopenharmony_ci   uint32_t offsets[4];
93bf215546Sopenharmony_ci   uint32_t row_pitches[4];
94bf215546Sopenharmony_ci#ifndef _WIN32
95bf215546Sopenharmony_ci   int dma_buf_fd;
96bf215546Sopenharmony_ci#endif
97bf215546Sopenharmony_ci   void *cpu_map;
98bf215546Sopenharmony_ci};
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_cistruct wsi_swapchain {
101bf215546Sopenharmony_ci   struct vk_object_base base;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   const struct wsi_device *wsi;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   VkDevice device;
106bf215546Sopenharmony_ci   VkAllocationCallbacks alloc;
107bf215546Sopenharmony_ci   VkFence* fences;
108bf215546Sopenharmony_ci   VkSemaphore* buffer_blit_semaphores;
109bf215546Sopenharmony_ci   VkPresentModeKHR present_mode;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   int signal_dma_buf_from_semaphore;
112bf215546Sopenharmony_ci   VkSemaphore dma_buf_semaphore;
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   struct wsi_image_info image_info;
115bf215546Sopenharmony_ci   uint32_t image_count;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci   bool use_buffer_blit;
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   /* If the driver wants to use a special queue to execute the buffer blit,
120bf215546Sopenharmony_ci    * it'll implement the wsi_device::get_buffer_blit_queue callback.
121bf215546Sopenharmony_ci    * The created queue will be stored here and will be used to execute the
122bf215546Sopenharmony_ci    * buffer blit instead of using the present queue.
123bf215546Sopenharmony_ci    */
124bf215546Sopenharmony_ci   VkQueue buffer_blit_queue;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   /* Command pools, one per queue family */
127bf215546Sopenharmony_ci   VkCommandPool *cmd_pools;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   VkResult (*destroy)(struct wsi_swapchain *swapchain,
130bf215546Sopenharmony_ci                       const VkAllocationCallbacks *pAllocator);
131bf215546Sopenharmony_ci   struct wsi_image *(*get_wsi_image)(struct wsi_swapchain *swapchain,
132bf215546Sopenharmony_ci                                      uint32_t image_index);
133bf215546Sopenharmony_ci   VkResult (*acquire_next_image)(struct wsi_swapchain *swap_chain,
134bf215546Sopenharmony_ci                                  const VkAcquireNextImageInfoKHR *info,
135bf215546Sopenharmony_ci                                  uint32_t *image_index);
136bf215546Sopenharmony_ci   VkResult (*queue_present)(struct wsi_swapchain *swap_chain,
137bf215546Sopenharmony_ci                             uint32_t image_index,
138bf215546Sopenharmony_ci                             const VkPresentRegionKHR *damage);
139bf215546Sopenharmony_ci};
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_cibool
142bf215546Sopenharmony_ciwsi_device_matches_drm_fd(const struct wsi_device *wsi, int drm_fd);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ciVkResult
145bf215546Sopenharmony_ciwsi_swapchain_init(const struct wsi_device *wsi,
146bf215546Sopenharmony_ci                   struct wsi_swapchain *chain,
147bf215546Sopenharmony_ci                   VkDevice device,
148bf215546Sopenharmony_ci                   const VkSwapchainCreateInfoKHR *pCreateInfo,
149bf215546Sopenharmony_ci                   const VkAllocationCallbacks *pAllocator,
150bf215546Sopenharmony_ci                   bool use_buffer_blit);
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_cienum VkPresentModeKHR
153bf215546Sopenharmony_ciwsi_swapchain_get_present_mode(struct wsi_device *wsi,
154bf215546Sopenharmony_ci                               const VkSwapchainCreateInfoKHR *pCreateInfo);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_civoid wsi_swapchain_finish(struct wsi_swapchain *chain);
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ciuint32_t
159bf215546Sopenharmony_ciwsi_select_memory_type(const struct wsi_device *wsi,
160bf215546Sopenharmony_ci                       VkMemoryPropertyFlags req_flags,
161bf215546Sopenharmony_ci                       VkMemoryPropertyFlags deny_flags,
162bf215546Sopenharmony_ci                       uint32_t type_bits);
163bf215546Sopenharmony_ciuint32_t
164bf215546Sopenharmony_ciwsi_select_device_memory_type(const struct wsi_device *wsi,
165bf215546Sopenharmony_ci                              uint32_t type_bits);
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ciVkResult
168bf215546Sopenharmony_ciwsi_configure_native_image(const struct wsi_swapchain *chain,
169bf215546Sopenharmony_ci                           const VkSwapchainCreateInfoKHR *pCreateInfo,
170bf215546Sopenharmony_ci                           uint32_t num_modifier_lists,
171bf215546Sopenharmony_ci                           const uint32_t *num_modifiers,
172bf215546Sopenharmony_ci                           const uint64_t *const *modifiers,
173bf215546Sopenharmony_ci                           struct wsi_image_info *info);
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ciVkResult
176bf215546Sopenharmony_ciwsi_configure_prime_image(UNUSED const struct wsi_swapchain *chain,
177bf215546Sopenharmony_ci                          const VkSwapchainCreateInfoKHR *pCreateInfo,
178bf215546Sopenharmony_ci                          bool use_modifier,
179bf215546Sopenharmony_ci                          struct wsi_image_info *info);
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ciVkResult
182bf215546Sopenharmony_ciwsi_configure_cpu_image(const struct wsi_swapchain *chain,
183bf215546Sopenharmony_ci                        const VkSwapchainCreateInfoKHR *pCreateInfo,
184bf215546Sopenharmony_ci                        uint8_t *(alloc_shm)(struct wsi_image *image,
185bf215546Sopenharmony_ci                                             unsigned size),
186bf215546Sopenharmony_ci                        struct wsi_image_info *info);
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ciVkResult
189bf215546Sopenharmony_ciwsi_create_buffer_image_mem(const struct wsi_swapchain *chain,
190bf215546Sopenharmony_ci                            const struct wsi_image_info *info,
191bf215546Sopenharmony_ci                            struct wsi_image *image,
192bf215546Sopenharmony_ci                            VkExternalMemoryHandleTypeFlags handle_types,
193bf215546Sopenharmony_ci                            bool implicit_sync);
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ciVkResult
196bf215546Sopenharmony_ciwsi_finish_create_buffer_image(const struct wsi_swapchain *chain,
197bf215546Sopenharmony_ci                               const struct wsi_image_info *info,
198bf215546Sopenharmony_ci                               struct wsi_image *image);
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ciVkResult
201bf215546Sopenharmony_ciwsi_configure_buffer_image(UNUSED const struct wsi_swapchain *chain,
202bf215546Sopenharmony_ci                           const VkSwapchainCreateInfoKHR *pCreateInfo,
203bf215546Sopenharmony_ci                           uint32_t stride_align, uint32_t size_align,
204bf215546Sopenharmony_ci                           struct wsi_image_info *info);
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ciVkResult
207bf215546Sopenharmony_ciwsi_configure_image(const struct wsi_swapchain *chain,
208bf215546Sopenharmony_ci                    const VkSwapchainCreateInfoKHR *pCreateInfo,
209bf215546Sopenharmony_ci                    VkExternalMemoryHandleTypeFlags handle_types,
210bf215546Sopenharmony_ci                    struct wsi_image_info *info);
211bf215546Sopenharmony_civoid
212bf215546Sopenharmony_ciwsi_destroy_image_info(const struct wsi_swapchain *chain,
213bf215546Sopenharmony_ci                       struct wsi_image_info *info);
214bf215546Sopenharmony_ciVkResult
215bf215546Sopenharmony_ciwsi_create_image(const struct wsi_swapchain *chain,
216bf215546Sopenharmony_ci                 const struct wsi_image_info *info,
217bf215546Sopenharmony_ci                 struct wsi_image *image);
218bf215546Sopenharmony_civoid
219bf215546Sopenharmony_ciwsi_image_init(struct wsi_image *image);
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_civoid
222bf215546Sopenharmony_ciwsi_destroy_image(const struct wsi_swapchain *chain,
223bf215546Sopenharmony_ci                  struct wsi_image *image);
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci#ifdef HAVE_LIBDRM
226bf215546Sopenharmony_ciVkResult
227bf215546Sopenharmony_ciwsi_prepare_signal_dma_buf_from_semaphore(struct wsi_swapchain *chain,
228bf215546Sopenharmony_ci                                          const struct wsi_image *image);
229bf215546Sopenharmony_ciVkResult
230bf215546Sopenharmony_ciwsi_signal_dma_buf_from_semaphore(const struct wsi_swapchain *chain,
231bf215546Sopenharmony_ci                                  const struct wsi_image *image);
232bf215546Sopenharmony_ciVkResult
233bf215546Sopenharmony_ciwsi_create_sync_for_dma_buf_wait(const struct wsi_swapchain *chain,
234bf215546Sopenharmony_ci                                 const struct wsi_image *image,
235bf215546Sopenharmony_ci                                 enum vk_sync_features sync_features,
236bf215546Sopenharmony_ci                                 struct vk_sync **sync_out);
237bf215546Sopenharmony_ci#endif
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_cistruct wsi_interface {
240bf215546Sopenharmony_ci   VkResult (*get_support)(VkIcdSurfaceBase *surface,
241bf215546Sopenharmony_ci                           struct wsi_device *wsi_device,
242bf215546Sopenharmony_ci                           uint32_t queueFamilyIndex,
243bf215546Sopenharmony_ci                           VkBool32* pSupported);
244bf215546Sopenharmony_ci   VkResult (*get_capabilities2)(VkIcdSurfaceBase *surface,
245bf215546Sopenharmony_ci                                 struct wsi_device *wsi_device,
246bf215546Sopenharmony_ci                                 const void *info_next,
247bf215546Sopenharmony_ci                                 VkSurfaceCapabilities2KHR* pSurfaceCapabilities);
248bf215546Sopenharmony_ci   VkResult (*get_formats)(VkIcdSurfaceBase *surface,
249bf215546Sopenharmony_ci                           struct wsi_device *wsi_device,
250bf215546Sopenharmony_ci                           uint32_t* pSurfaceFormatCount,
251bf215546Sopenharmony_ci                           VkSurfaceFormatKHR* pSurfaceFormats);
252bf215546Sopenharmony_ci   VkResult (*get_formats2)(VkIcdSurfaceBase *surface,
253bf215546Sopenharmony_ci                            struct wsi_device *wsi_device,
254bf215546Sopenharmony_ci                            const void *info_next,
255bf215546Sopenharmony_ci                            uint32_t* pSurfaceFormatCount,
256bf215546Sopenharmony_ci                            VkSurfaceFormat2KHR* pSurfaceFormats);
257bf215546Sopenharmony_ci   VkResult (*get_present_modes)(VkIcdSurfaceBase *surface,
258bf215546Sopenharmony_ci                                 uint32_t* pPresentModeCount,
259bf215546Sopenharmony_ci                                 VkPresentModeKHR* pPresentModes);
260bf215546Sopenharmony_ci   VkResult (*get_present_rectangles)(VkIcdSurfaceBase *surface,
261bf215546Sopenharmony_ci                                      struct wsi_device *wsi_device,
262bf215546Sopenharmony_ci                                      uint32_t* pRectCount,
263bf215546Sopenharmony_ci                                      VkRect2D* pRects);
264bf215546Sopenharmony_ci   VkResult (*create_swapchain)(VkIcdSurfaceBase *surface,
265bf215546Sopenharmony_ci                                VkDevice device,
266bf215546Sopenharmony_ci                                struct wsi_device *wsi_device,
267bf215546Sopenharmony_ci                                const VkSwapchainCreateInfoKHR* pCreateInfo,
268bf215546Sopenharmony_ci                                const VkAllocationCallbacks* pAllocator,
269bf215546Sopenharmony_ci                                struct wsi_swapchain **swapchain);
270bf215546Sopenharmony_ci};
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ciVkResult wsi_x11_init_wsi(struct wsi_device *wsi_device,
273bf215546Sopenharmony_ci                          const VkAllocationCallbacks *alloc,
274bf215546Sopenharmony_ci                          const struct driOptionCache *dri_options);
275bf215546Sopenharmony_civoid wsi_x11_finish_wsi(struct wsi_device *wsi_device,
276bf215546Sopenharmony_ci                        const VkAllocationCallbacks *alloc);
277bf215546Sopenharmony_ciVkResult wsi_wl_init_wsi(struct wsi_device *wsi_device,
278bf215546Sopenharmony_ci                         const VkAllocationCallbacks *alloc,
279bf215546Sopenharmony_ci                         VkPhysicalDevice physical_device);
280bf215546Sopenharmony_civoid wsi_wl_finish_wsi(struct wsi_device *wsi_device,
281bf215546Sopenharmony_ci                       const VkAllocationCallbacks *alloc);
282bf215546Sopenharmony_ciVkResult wsi_win32_init_wsi(struct wsi_device *wsi_device,
283bf215546Sopenharmony_ci                         const VkAllocationCallbacks *alloc,
284bf215546Sopenharmony_ci                         VkPhysicalDevice physical_device);
285bf215546Sopenharmony_civoid wsi_win32_finish_wsi(struct wsi_device *wsi_device,
286bf215546Sopenharmony_ci                       const VkAllocationCallbacks *alloc);
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ciVkResult
290bf215546Sopenharmony_ciwsi_display_init_wsi(struct wsi_device *wsi_device,
291bf215546Sopenharmony_ci                     const VkAllocationCallbacks *alloc,
292bf215546Sopenharmony_ci                     int display_fd);
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_civoid
295bf215546Sopenharmony_ciwsi_display_finish_wsi(struct wsi_device *wsi_device,
296bf215546Sopenharmony_ci                       const VkAllocationCallbacks *alloc);
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_civoid
299bf215546Sopenharmony_ciwsi_display_setup_syncobj_fd(struct wsi_device *wsi_device,
300bf215546Sopenharmony_ci                             int fd);
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ciVK_DEFINE_NONDISP_HANDLE_CASTS(wsi_swapchain, base, VkSwapchainKHR,
303bf215546Sopenharmony_ci                               VK_OBJECT_TYPE_SWAPCHAIN_KHR)
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci#endif /* WSI_COMMON_PRIVATE_H */
306