xref: /aosp_15_r20/external/mesa3d/src/vulkan/wsi/wsi_common.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2015 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_H
24 #define WSI_COMMON_H
25 
26 #include <stdint.h>
27 #include <stdbool.h>
28 
29 #include "util/log.h"
30 #include "vk_alloc.h"
31 #include "vk_dispatch_table.h"
32 #include <vulkan/vulkan.h>
33 #include <vulkan/vk_icd.h>
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #ifndef WSI_ENTRYPOINTS_H
40 extern const struct vk_instance_entrypoint_table wsi_instance_entrypoints;
41 extern const struct vk_physical_device_entrypoint_table wsi_physical_device_entrypoints;
42 extern const struct vk_device_entrypoint_table wsi_device_entrypoints;
43 #endif
44 
45 #include <util/list.h>
46 
47 /* This is guaranteed to not collide with anything because it's in the
48  * VK_KHR_swapchain namespace but not actually used by the extension.
49  */
50 #define VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA (VkStructureType)1000001002
51 #define VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA (VkStructureType)1000001003
52 #define VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA (VkStructureType)1000001005
53 #define VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA (VkStructureType)1000001006
54 
55 #define VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA_cast struct wsi_image_create_info
56 #define VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA_cast struct wsi_memory_allocate_info
57 #define VK_STRUCTURE_TYPE_WSI_SURFACE_SUPPORTED_COUNTERS_MESA_cast struct wsi_surface_supported_counters
58 #define VK_STRUCTURE_TYPE_WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA_cast struct wsi_memory_signal_submit_info
59 
60 /* This is always chained to VkImageCreateInfo when a wsi image is created.
61  * It indicates that the image can be transitioned to/from
62  * VK_IMAGE_LAYOUT_PRESENT_SRC_KHR.
63  */
64 struct wsi_image_create_info {
65     VkStructureType sType;
66     const void *pNext;
67     bool scanout;
68 
69     /* if true, the image is a blit source */
70     bool blit_src;
71 };
72 
73 struct wsi_memory_allocate_info {
74     VkStructureType sType;
75     const void *pNext;
76     bool implicit_sync;
77 };
78 
79 /* To be chained into VkSurfaceCapabilities2KHR */
80 struct wsi_surface_supported_counters {
81    VkStructureType sType;
82    const void *pNext;
83 
84    VkSurfaceCounterFlagsEXT supported_surface_counters;
85 
86 };
87 
88 /* To be chained into VkSubmitInfo */
89 struct wsi_memory_signal_submit_info {
90     VkStructureType sType;
91     const void *pNext;
92     VkDeviceMemory memory;
93 };
94 
95 struct wsi_interface;
96 struct vk_instance;
97 
98 struct driOptionCache;
99 
100 #define VK_ICD_WSI_PLATFORM_MAX (VK_ICD_WSI_PLATFORM_METAL + 1)
101 
102 struct wsi_device {
103    /* Allocator for the instance */
104    VkAllocationCallbacks instance_alloc;
105 
106    VkPhysicalDevice pdevice;
107    VkPhysicalDeviceMemoryProperties memory_props;
108    uint32_t queue_family_count;
109    uint64_t queue_supports_blit;
110 
111    VkPhysicalDeviceDrmPropertiesEXT drm_info;
112    VkPhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info;
113 
114    VkExternalSemaphoreHandleTypeFlags semaphore_export_handle_types;
115    VkExternalSemaphoreHandleTypeFlags timeline_semaphore_export_handle_types;
116 
117    bool has_import_memory_host;
118    bool has_timeline_semaphore;
119 
120    /** Indicates if wsi_image_create_info::scanout is supported
121     *
122     * If false, WSI will always use either modifiers or the prime blit path.
123     */
124    bool supports_scanout;
125    bool supports_modifiers;
126    uint32_t maxImageDimension2D;
127    uint32_t optimalBufferCopyRowPitchAlignment;
128    VkPresentModeKHR override_present_mode;
129    bool force_bgra8_unorm_first;
130 
131    /* Whether to enable adaptive sync for a swapchain if implemented and
132     * available. Not all window systems might support this. */
133    bool enable_adaptive_sync;
134 
135    /* List of fences to signal when hotplug event happens. */
136    struct list_head hotplug_fences;
137 
138    /* Create headless swapchains. */
139    bool force_headless_swapchain;
140 
141    bool force_swapchain_to_currentExtent;
142 
143    struct {
144       /* Override the minimum number of images on the swapchain.
145        * 0 = no override */
146       uint32_t override_minImageCount;
147 
148       /* Forces strict number of image on the swapchain using application
149        * provided VkSwapchainCreateInfoKH::RminImageCount.
150        */
151       bool strict_imageCount;
152 
153       /* Ensures to create at least the number of image specified by the
154        * driver in VkSurfaceCapabilitiesKHR::minImageCount.
155        */
156       bool ensure_minImageCount;
157 
158       /* Wait for fences before submitting buffers to Xwayland. Defaults to
159        * true.
160        */
161       bool xwaylandWaitReady;
162 
163       /* adds an extra minImageCount when running under xwayland */
164       bool extra_xwayland_image;
165 
166       /* Never report VK_SUBOPTIMAL_KHR. Used to workaround
167        * games that cannot handle SUBOPTIMAL correctly. */
168       bool ignore_suboptimal;
169    } x11;
170 
171    struct {
172       void *(*get_d3d12_command_queue)(VkDevice device);
173       /* Needs to be per VkDevice, not VkPhysicalDevice, depends on queue config */
174       bool (*requires_blits)(VkDevice device);
175       VkResult (*create_image_memory)(VkDevice device, void *resource,
176                                       const VkAllocationCallbacks *alloc,
177                                       VkDeviceMemory *out);
178    } win32;
179 
180    bool sw;
181 
182    /* Set to true if the implementation is ok with linear WSI images. */
183    bool wants_linear;
184 
185    /* Signals the semaphore such that any wait on the semaphore will wait on
186     * any reads or writes on the give memory object.  This is used to
187     * implement the semaphore signal operation in vkAcquireNextImage.  This
188     * requires the driver to implement vk_device::create_sync_for_memory.
189     */
190    bool signal_semaphore_with_memory;
191 
192    /* Signals the fence such that any wait on the fence will wait on any reads
193     * or writes on the give memory object.  This is used to implement the
194     * semaphore signal operation in vkAcquireNextImage.  This requires the
195     * driver to implement vk_device::create_sync_for_memory.  The resulting
196     * vk_sync must support CPU waits.
197     */
198    bool signal_fence_with_memory;
199 
200    /* Whether present_wait functionality is enabled on the device.
201     * In this case, we have to create an extra timeline semaphore
202     * to be able to synchronize with the WSI present semaphore being unsignalled.
203     * This requires VK_KHR_timeline_semaphore. */
204    bool khr_present_wait;
205 
206    /*
207     * This sets the ownership for a WSI memory object:
208     *
209     * The ownership is true if and only if the application is allowed to submit
210     * command buffers that reference the buffer.
211     *
212     * This can be used to prune BO lists without too many adverse affects on
213     * implicit sync.
214     *
215     * Side note: care needs to be taken for internally delayed submissions wrt
216     * timeline semaphores.
217     */
218    void (*set_memory_ownership)(VkDevice device,
219                                 VkDeviceMemory memory,
220                                 VkBool32 ownership);
221 
222    /*
223     * If this is set, the WSI device will call it to let the driver backend
224     * decide if it can present images directly on the given device fd.
225     */
226    bool (*can_present_on_device)(VkPhysicalDevice pdevice, int fd);
227 
228    /*
229     * A driver can implement this callback to return a special queue to execute
230     * buffer blits.
231     */
232    VkQueue (*get_blit_queue)(VkDevice device);
233 
234 #define WSI_CB(cb) PFN_vk##cb cb
235    WSI_CB(AllocateMemory);
236    WSI_CB(AllocateCommandBuffers);
237    WSI_CB(BindBufferMemory);
238    WSI_CB(BindImageMemory);
239    WSI_CB(BeginCommandBuffer);
240    WSI_CB(CmdPipelineBarrier);
241    WSI_CB(CmdCopyImage);
242    WSI_CB(CmdCopyImageToBuffer);
243    WSI_CB(CreateBuffer);
244    WSI_CB(CreateCommandPool);
245    WSI_CB(CreateFence);
246    WSI_CB(CreateImage);
247    WSI_CB(CreateSemaphore);
248    WSI_CB(DestroyBuffer);
249    WSI_CB(DestroyCommandPool);
250    WSI_CB(DestroyFence);
251    WSI_CB(DestroyImage);
252    WSI_CB(DestroySemaphore);
253    WSI_CB(EndCommandBuffer);
254    WSI_CB(FreeMemory);
255    WSI_CB(FreeCommandBuffers);
256    WSI_CB(GetBufferMemoryRequirements);
257    WSI_CB(GetFenceStatus);
258    WSI_CB(GetImageDrmFormatModifierPropertiesEXT);
259    WSI_CB(GetImageMemoryRequirements);
260    WSI_CB(GetImageSubresourceLayout);
261    WSI_CB(GetMemoryFdKHR);
262    WSI_CB(GetPhysicalDeviceFormatProperties);
263    WSI_CB(GetPhysicalDeviceFormatProperties2);
264    WSI_CB(GetPhysicalDeviceImageFormatProperties2);
265    WSI_CB(GetSemaphoreFdKHR);
266    WSI_CB(ResetFences);
267    WSI_CB(QueueSubmit);
268    WSI_CB(WaitForFences);
269    WSI_CB(MapMemory);
270    WSI_CB(UnmapMemory);
271    WSI_CB(WaitSemaphores);
272 #undef WSI_CB
273 
274     struct wsi_interface *                  wsi[VK_ICD_WSI_PLATFORM_MAX];
275 };
276 
277 typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName);
278 
279 struct wsi_device_options {
280    bool sw_device;
281    bool extra_xwayland_image;
282 };
283 
284 VkResult
285 wsi_device_init(struct wsi_device *wsi,
286                 VkPhysicalDevice pdevice,
287                 WSI_FN_GetPhysicalDeviceProcAddr proc_addr,
288                 const VkAllocationCallbacks *alloc,
289                 int display_fd,
290                 const struct driOptionCache *dri_options,
291                 const struct wsi_device_options *device_options);
292 
293 void
294 wsi_device_finish(struct wsi_device *wsi,
295                   const VkAllocationCallbacks *alloc);
296 
297 /* Setup file descriptor to be used with imported sync_fd's in wsi fences. */
298 void
299 wsi_device_setup_syncobj_fd(struct wsi_device *wsi_device,
300                             int fd);
301 
302 #define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType)             \
303                                                                            \
304    static inline __VkIcdType *                                             \
305    __VkIcdType ## _from_handle(__VkType _handle)                           \
306    {                                                                       \
307       return (__VkIcdType *)(uintptr_t) _handle;                           \
308    }                                                                       \
309                                                                            \
310    static inline __VkType                                                  \
311    __VkIcdType ## _to_handle(__VkIcdType *_obj)                            \
312    {                                                                       \
313       return (__VkType)(uintptr_t) _obj;                                   \
314    }
315 
316 #define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \
317    __VkIcdType *__name = __VkIcdType ## _from_handle(__handle)
318 
319 ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR)
320 
321 VkResult
322 wsi_common_get_images(VkSwapchainKHR _swapchain,
323                       uint32_t *pSwapchainImageCount,
324                       VkImage *pSwapchainImages);
325 
326 VkImage
327 wsi_common_get_image(VkSwapchainKHR _swapchain, uint32_t index);
328 
329 VkResult
330 wsi_common_acquire_next_image2(const struct wsi_device *wsi,
331                                VkDevice device,
332                                const VkAcquireNextImageInfoKHR *pAcquireInfo,
333                                uint32_t *pImageIndex);
334 
335 VkResult
336 wsi_common_queue_present(const struct wsi_device *wsi,
337                          VkDevice device_h,
338                          VkQueue queue_h,
339                          int queue_family_index,
340                          const VkPresentInfoKHR *pPresentInfo);
341 
342 VkResult
343 wsi_common_create_swapchain_image(const struct wsi_device *wsi,
344                                   const VkImageCreateInfo *pCreateInfo,
345                                   VkSwapchainKHR _swapchain,
346                                   VkImage *pImage);
347 VkResult
348 wsi_common_bind_swapchain_image(const struct wsi_device *wsi,
349                                 VkImage vk_image,
350                                 VkSwapchainKHR _swapchain,
351                                 uint32_t image_idx);
352 
353 bool
354 wsi_common_vk_instance_supports_present_wait(const struct vk_instance *instance);
355 
356 VkImageUsageFlags
357 wsi_caps_get_image_usage(void);
358 
359 bool
360 wsi_device_supports_explicit_sync(struct wsi_device *device);
361 
362 #define wsi_common_vk_warn_once(warning) \
363    do { \
364       static int warned = false; \
365       if (!warned) { \
366          mesa_loge(warning); \
367          warned = true; \
368       } \
369    } while (0)
370 
371 #ifdef __cplusplus
372 }
373 #endif
374 
375 #endif
376