xref: /aosp_15_r20/external/mesa3d/src/vulkan/wsi/wsi_common_private.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "util/perf/cpu_trace.h"
28 #include "vk_object.h"
29 #include "vk_sync.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 struct wsi_image;
36 struct wsi_swapchain;
37 
38 #define WSI_DEBUG_BUFFER      (1ull << 0)
39 #define WSI_DEBUG_SW          (1ull << 1)
40 #define WSI_DEBUG_NOSHM       (1ull << 2)
41 #define WSI_DEBUG_LINEAR      (1ull << 3)
42 #define WSI_DEBUG_DXGI        (1ull << 4)
43 
44 extern uint64_t WSI_DEBUG;
45 
46 enum wsi_image_type {
47    WSI_IMAGE_TYPE_CPU,
48    WSI_IMAGE_TYPE_DRM,
49    WSI_IMAGE_TYPE_DXGI,
50 };
51 
52 struct wsi_base_image_params {
53    enum wsi_image_type image_type;
54 };
55 
56 struct wsi_cpu_image_params {
57    struct wsi_base_image_params base;
58 
59    uint8_t *(*alloc_shm)(struct wsi_image *image, unsigned size);
60 };
61 
62 struct wsi_drm_image_params {
63    struct wsi_base_image_params base;
64 
65    bool same_gpu;
66    bool explicit_sync;
67 
68    uint32_t num_modifier_lists;
69    const uint32_t *num_modifiers;
70    const uint64_t *const *modifiers;
71 };
72 
73 struct wsi_dxgi_image_params {
74    struct wsi_base_image_params base;
75    bool storage_image;
76 };
77 
78 typedef uint32_t (*wsi_memory_type_select_cb)(const struct wsi_device *wsi,
79                                               uint32_t type_bits);
80 
81 struct wsi_image_info {
82    VkImageCreateInfo create;
83    struct wsi_image_create_info wsi;
84    VkExternalMemoryImageCreateInfo ext_mem;
85    VkImageFormatListCreateInfo format_list;
86    VkImageDrmFormatModifierListCreateInfoEXT drm_mod_list;
87 
88    enum wsi_image_type image_type;
89    bool explicit_sync;
90    bool prime_use_linear_modifier;
91 
92    /* Not really part of VkImageCreateInfo but needed to figure out the
93     * number of planes we need to bind.
94     */
95    uint32_t modifier_prop_count;
96    struct VkDrmFormatModifierPropertiesEXT *modifier_props;
97 
98    /* For buffer blit images, the linear stride in bytes */
99    uint32_t linear_stride;
100 
101    /* For buffer blit images, the size of the buffer in bytes */
102    uint64_t linear_size;
103 
104    wsi_memory_type_select_cb select_image_memory_type;
105    wsi_memory_type_select_cb select_blit_dst_memory_type;
106 
107    uint8_t *(*alloc_shm)(struct wsi_image *image, unsigned size);
108 
109    VkResult (*create_mem)(const struct wsi_swapchain *chain,
110                           const struct wsi_image_info *info,
111                           struct wsi_image *image);
112 
113    VkResult (*finish_create)(const struct wsi_swapchain *chain,
114                              const struct wsi_image_info *info,
115                              struct wsi_image *image);
116 };
117 
118 enum wsi_explicit_sync_timelines
119 {
120    WSI_ES_ACQUIRE,
121    WSI_ES_RELEASE,
122 
123    WSI_ES_COUNT,
124 };
125 
126 struct wsi_image_explicit_sync_timeline {
127    VkSemaphore semaphore;
128    uint64_t timeline;
129    int fd;
130    uint32_t handle;
131 };
132 
133 enum wsi_swapchain_blit_type {
134    WSI_SWAPCHAIN_NO_BLIT,
135    WSI_SWAPCHAIN_BUFFER_BLIT,
136    WSI_SWAPCHAIN_IMAGE_BLIT,
137 };
138 
139 struct wsi_image {
140    VkImage image;
141    VkDeviceMemory memory;
142 
143    struct {
144       VkBuffer buffer;
145       VkImage image;
146       VkDeviceMemory memory;
147       VkCommandBuffer *cmd_buffers;
148    } blit;
149    /* Whether or not the image has been acquired
150     * on the CPU side via acquire_next_image.
151     */
152    bool acquired;
153    uint64_t present_serial;
154 
155    struct wsi_image_explicit_sync_timeline explicit_sync[WSI_ES_COUNT];
156 
157 #ifndef _WIN32
158    uint64_t drm_modifier;
159 #endif
160    int num_planes;
161    uint32_t sizes[4];
162    uint32_t offsets[4];
163    uint32_t row_pitches[4];
164 #ifndef _WIN32
165    int dma_buf_fd;
166 #endif
167    void *cpu_map;
168 };
169 
170 struct wsi_swapchain {
171    struct vk_object_base base;
172 
173    const struct wsi_device *wsi;
174 
175    VkDevice device;
176    VkAllocationCallbacks alloc;
177    VkFence* fences;
178    VkPresentModeKHR present_mode;
179    VkSemaphore present_id_timeline;
180 
181    int signal_dma_buf_from_semaphore;
182    VkSemaphore dma_buf_semaphore;
183 
184    struct wsi_image_info image_info;
185    uint32_t image_count;
186 
187    uint64_t present_serial;
188 
189    struct {
190       enum wsi_swapchain_blit_type type;
191       VkSemaphore *semaphores;
192 
193       /* If the driver wants to use a special queue to execute the buffer blit,
194        * it'll implement the wsi_device::get_blit_queue callback.
195        * The created queue will be stored here and will be used to execute the
196        * buffer blit instead of using the present queue.
197        */
198       VkQueue queue;
199    } blit;
200 
201    bool capture_key_pressed;
202 
203    /* Command pools, one per queue family */
204    VkCommandPool *cmd_pools;
205 
206    VkResult (*destroy)(struct wsi_swapchain *swapchain,
207                        const VkAllocationCallbacks *pAllocator);
208    struct wsi_image *(*get_wsi_image)(struct wsi_swapchain *swapchain,
209                                       uint32_t image_index);
210    VkResult (*acquire_next_image)(struct wsi_swapchain *swap_chain,
211                                   const VkAcquireNextImageInfoKHR *info,
212                                   uint32_t *image_index);
213    VkResult (*queue_present)(struct wsi_swapchain *swap_chain,
214                              uint32_t image_index,
215                              uint64_t present_id,
216                              const VkPresentRegionKHR *damage);
217    VkResult (*wait_for_present)(struct wsi_swapchain *swap_chain,
218                                 uint64_t present_id,
219                                 uint64_t timeout);
220    VkResult (*release_images)(struct wsi_swapchain *swap_chain,
221                               uint32_t count,
222                               const uint32_t *indices);
223    void (*set_present_mode)(struct wsi_swapchain *swap_chain,
224                             VkPresentModeKHR mode);
225 };
226 
227 bool
228 wsi_device_matches_drm_fd(VkPhysicalDevice pdevice, int drm_fd);
229 
230 void
231 wsi_wl_surface_destroy(VkIcdSurfaceBase *icd_surface, VkInstance _instance,
232                        const VkAllocationCallbacks *pAllocator);
233 
234 void
235 wsi_win32_surface_destroy(VkIcdSurfaceBase *icd_surface, VkInstance _instance,
236                           const VkAllocationCallbacks *pAllocator);
237 
238 VkResult
239 wsi_swapchain_init(const struct wsi_device *wsi,
240                    struct wsi_swapchain *chain,
241                    VkDevice device,
242                    const VkSwapchainCreateInfoKHR *pCreateInfo,
243                    const struct wsi_base_image_params *image_params,
244                    const VkAllocationCallbacks *pAllocator);
245 
246 enum VkPresentModeKHR
247 wsi_swapchain_get_present_mode(struct wsi_device *wsi,
248                                const VkSwapchainCreateInfoKHR *pCreateInfo);
249 
250 void wsi_swapchain_finish(struct wsi_swapchain *chain);
251 
252 uint32_t
253 wsi_select_memory_type(const struct wsi_device *wsi,
254                        VkMemoryPropertyFlags req_flags,
255                        VkMemoryPropertyFlags deny_flags,
256                        uint32_t type_bits);
257 uint32_t
258 wsi_select_device_memory_type(const struct wsi_device *wsi,
259                               uint32_t type_bits);
260 
261 bool
262 wsi_drm_image_needs_buffer_blit(const struct wsi_device *wsi,
263                                 const struct wsi_drm_image_params *params);
264 
265 enum wsi_swapchain_blit_type
266 wsi_dxgi_image_needs_blit(const struct wsi_device *wsi,
267                           const struct wsi_dxgi_image_params *params,
268                           VkDevice device);
269 
270 VkResult
271 wsi_drm_configure_image(const struct wsi_swapchain *chain,
272                         const VkSwapchainCreateInfoKHR *pCreateInfo,
273                         const struct wsi_drm_image_params *params,
274                         struct wsi_image_info *info);
275 
276 VkResult
277 wsi_dxgi_configure_image(const struct wsi_swapchain *chain,
278                          const VkSwapchainCreateInfoKHR *pCreateInfo,
279                          const struct wsi_dxgi_image_params *params,
280                          struct wsi_image_info *info);
281 
282 bool
283 wsi_cpu_image_needs_buffer_blit(const struct wsi_device *wsi,
284                                 const struct wsi_cpu_image_params *params);
285 
286 VkResult
287 wsi_configure_cpu_image(const struct wsi_swapchain *chain,
288                         const VkSwapchainCreateInfoKHR *pCreateInfo,
289                         const struct wsi_cpu_image_params *params,
290                         struct wsi_image_info *info);
291 
292 VkResult
293 wsi_create_buffer_blit_context(const struct wsi_swapchain *chain,
294                                const struct wsi_image_info *info,
295                                struct wsi_image *image,
296                                VkExternalMemoryHandleTypeFlags handle_types);
297 
298 VkResult
299 wsi_finish_create_blit_context(const struct wsi_swapchain *chain,
300                                const struct wsi_image_info *info,
301                                struct wsi_image *image);
302 
303 void
304 wsi_configure_buffer_image(UNUSED const struct wsi_swapchain *chain,
305                            const VkSwapchainCreateInfoKHR *pCreateInfo,
306                            uint32_t stride_align, uint32_t size_align,
307                            struct wsi_image_info *info);
308 
309 void
310 wsi_configure_image_blit_image(UNUSED const struct wsi_swapchain *chain,
311                                struct wsi_image_info *info);
312 
313 VkResult
314 wsi_configure_image(const struct wsi_swapchain *chain,
315                     const VkSwapchainCreateInfoKHR *pCreateInfo,
316                     VkExternalMemoryHandleTypeFlags handle_types,
317                     struct wsi_image_info *info);
318 void
319 wsi_destroy_image_info(const struct wsi_swapchain *chain,
320                        struct wsi_image_info *info);
321 VkResult
322 wsi_create_image(const struct wsi_swapchain *chain,
323                  const struct wsi_image_info *info,
324                  struct wsi_image *image);
325 void
326 wsi_image_init(struct wsi_image *image);
327 
328 void
329 wsi_destroy_image(const struct wsi_swapchain *chain,
330                   struct wsi_image *image);
331 
332 VkResult
333 wsi_swapchain_wait_for_present_semaphore(const struct wsi_swapchain *chain,
334                                          uint64_t present_id, uint64_t timeout);
335 
336 #ifdef HAVE_LIBDRM
337 VkResult
338 wsi_prepare_signal_dma_buf_from_semaphore(struct wsi_swapchain *chain,
339                                           const struct wsi_image *image);
340 VkResult
341 wsi_signal_dma_buf_from_semaphore(const struct wsi_swapchain *chain,
342                                   const struct wsi_image *image);
343 VkResult
344 wsi_create_sync_for_dma_buf_wait(const struct wsi_swapchain *chain,
345                                  const struct wsi_image *image,
346                                  enum vk_sync_features sync_features,
347                                  struct vk_sync **sync_out);
348 VkResult
349 wsi_create_sync_for_image_syncobj(const struct wsi_swapchain *chain,
350                                   const struct wsi_image *image,
351                                   enum vk_sync_features req_features,
352                                   struct vk_sync **sync_out);
353 
354 VkResult
355 wsi_create_image_explicit_sync_drm(const struct wsi_swapchain *chain,
356                                    struct wsi_image *image);
357 
358 void
359 wsi_destroy_image_explicit_sync_drm(const struct wsi_swapchain *chain,
360                                     struct wsi_image *image);
361 
362 VkResult
363 wsi_drm_wait_for_explicit_sync_release(struct wsi_swapchain *chain,
364                                        uint32_t image_count,
365                                        struct wsi_image **images,
366                                        uint64_t rel_timeout_ns,
367                                        uint32_t *image_index);
368 #endif
369 
370 struct wsi_interface {
371    VkResult (*get_support)(VkIcdSurfaceBase *surface,
372                            struct wsi_device *wsi_device,
373                            uint32_t queueFamilyIndex,
374                            VkBool32* pSupported);
375    VkResult (*get_capabilities2)(VkIcdSurfaceBase *surface,
376                                  struct wsi_device *wsi_device,
377                                  const void *info_next,
378                                  VkSurfaceCapabilities2KHR* pSurfaceCapabilities);
379    VkResult (*get_formats)(VkIcdSurfaceBase *surface,
380                            struct wsi_device *wsi_device,
381                            uint32_t* pSurfaceFormatCount,
382                            VkSurfaceFormatKHR* pSurfaceFormats);
383    VkResult (*get_formats2)(VkIcdSurfaceBase *surface,
384                             struct wsi_device *wsi_device,
385                             const void *info_next,
386                             uint32_t* pSurfaceFormatCount,
387                             VkSurfaceFormat2KHR* pSurfaceFormats);
388    VkResult (*get_present_modes)(VkIcdSurfaceBase *surface,
389                                  struct wsi_device *wsi_device,
390                                  uint32_t* pPresentModeCount,
391                                  VkPresentModeKHR* pPresentModes);
392    VkResult (*get_present_rectangles)(VkIcdSurfaceBase *surface,
393                                       struct wsi_device *wsi_device,
394                                       uint32_t* pRectCount,
395                                       VkRect2D* pRects);
396    VkResult (*create_swapchain)(VkIcdSurfaceBase *surface,
397                                 VkDevice device,
398                                 struct wsi_device *wsi_device,
399                                 const VkSwapchainCreateInfoKHR* pCreateInfo,
400                                 const VkAllocationCallbacks* pAllocator,
401                                 struct wsi_swapchain **swapchain);
402 };
403 
404 VkResult wsi_x11_init_wsi(struct wsi_device *wsi_device,
405                           const VkAllocationCallbacks *alloc,
406                           const struct driOptionCache *dri_options);
407 void wsi_x11_finish_wsi(struct wsi_device *wsi_device,
408                         const VkAllocationCallbacks *alloc);
409 VkResult wsi_wl_init_wsi(struct wsi_device *wsi_device,
410                          const VkAllocationCallbacks *alloc,
411                          VkPhysicalDevice physical_device);
412 void wsi_wl_finish_wsi(struct wsi_device *wsi_device,
413                        const VkAllocationCallbacks *alloc);
414 VkResult wsi_win32_init_wsi(struct wsi_device *wsi_device,
415                          const VkAllocationCallbacks *alloc,
416                          VkPhysicalDevice physical_device);
417 void wsi_win32_finish_wsi(struct wsi_device *wsi_device,
418                        const VkAllocationCallbacks *alloc);
419 VkResult wsi_metal_init_wsi(struct wsi_device *wsi_device,
420                            const VkAllocationCallbacks *alloc,
421                            VkPhysicalDevice physical_device);
422 void wsi_metal_finish_wsi(struct wsi_device *wsi_device,
423                           const VkAllocationCallbacks *alloc);
424 
425 
426 VkResult
427 wsi_display_init_wsi(struct wsi_device *wsi_device,
428                      const VkAllocationCallbacks *alloc,
429                      int display_fd);
430 
431 void
432 wsi_display_finish_wsi(struct wsi_device *wsi_device,
433                        const VkAllocationCallbacks *alloc);
434 
435 void
436 wsi_display_setup_syncobj_fd(struct wsi_device *wsi_device,
437                              int fd);
438 
439 VkResult wsi_headless_init_wsi(struct wsi_device *wsi_device,
440                                const VkAllocationCallbacks *alloc,
441                                VkPhysicalDevice physical_device);
442 
443 void wsi_headless_finish_wsi(struct wsi_device *wsi_device,
444                              const VkAllocationCallbacks *alloc);
445 
446 VK_DEFINE_NONDISP_HANDLE_CASTS(wsi_swapchain, base, VkSwapchainKHR,
447                                VK_OBJECT_TYPE_SWAPCHAIN_KHR)
448 
449 #ifdef __cplusplus
450 }
451 #endif
452 
453 #endif /* WSI_COMMON_PRIVATE_H */
454