• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_HEADLESS + 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 
110    VkPhysicalDeviceDrmPropertiesEXT drm_info;
111    VkPhysicalDevicePCIBusInfoPropertiesEXT pci_bus_info;
112 
113    VkExternalSemaphoreHandleTypeFlags semaphore_export_handle_types;
114 
115    bool has_import_memory_host;
116 
117    /** Indicates if wsi_image_create_info::scanout is supported
118     *
119     * If false, WSI will always use either modifiers or the prime blit path.
120     */
121    bool supports_scanout;
122    bool supports_modifiers;
123    uint32_t maxImageDimension2D;
124    uint32_t optimalBufferCopyRowPitchAlignment;
125    VkPresentModeKHR override_present_mode;
126    bool force_bgra8_unorm_first;
127 
128    /* Whether to enable adaptive sync for a swapchain if implemented and
129     * available. Not all window systems might support this. */
130    bool enable_adaptive_sync;
131 
132    /* List of fences to signal when hotplug event happens. */
133    struct list_head hotplug_fences;
134 
135    /* Create headless swapchains. */
136    bool force_headless_swapchain;
137 
138    bool force_swapchain_to_currentExtent;
139 
140    struct {
141       /* Override the minimum number of images on the swapchain.
142        * 0 = no override */
143       uint32_t override_minImageCount;
144 
145       /* Forces strict number of image on the swapchain using application
146        * provided VkSwapchainCreateInfoKH::RminImageCount.
147        */
148       bool strict_imageCount;
149 
150       /* Ensures to create at least the number of image specified by the
151        * driver in VkSurfaceCapabilitiesKHR::minImageCount.
152        */
153       bool ensure_minImageCount;
154 
155       /* Wait for fences before submitting buffers to Xwayland. Defaults to
156        * true.
157        */
158       bool xwaylandWaitReady;
159 
160       /* adds an extra minImageCount when running under xwayland */
161       bool extra_xwayland_image;
162    } x11;
163 
164    struct {
165       void *(*get_d3d12_command_queue)(VkDevice device);
166       /* Needs to be per VkDevice, not VkPhysicalDevice, depends on queue config */
167       bool (*requires_blits)(VkDevice device);
168       VkResult (*create_image_memory)(VkDevice device, void *resource,
169                                       const VkAllocationCallbacks *alloc,
170                                       VkDeviceMemory *out);
171    } win32;
172 
173    bool sw;
174 
175    /* Set to true if the implementation is ok with linear WSI images. */
176    bool wants_linear;
177 
178    /* Signals the semaphore such that any wait on the semaphore will wait on
179     * any reads or writes on the give memory object.  This is used to
180     * implement the semaphore signal operation in vkAcquireNextImage.  This
181     * requires the driver to implement vk_device::create_sync_for_memory.
182     */
183    bool signal_semaphore_with_memory;
184 
185    /* Signals the fence such that any wait on the fence will wait on any reads
186     * or writes on the give memory object.  This is used to implement the
187     * semaphore signal operation in vkAcquireNextImage.  This requires the
188     * driver to implement vk_device::create_sync_for_memory.  The resulting
189     * vk_sync must support CPU waits.
190     */
191    bool signal_fence_with_memory;
192 
193    /* Whether present_wait functionality is enabled on the device.
194     * In this case, we have to create an extra timeline semaphore
195     * to be able to synchronize with the WSI present semaphore being unsignalled.
196     * This requires VK_KHR_timeline_semaphore. */
197    bool khr_present_wait;
198 
199    /*
200     * This sets the ownership for a WSI memory object:
201     *
202     * The ownership is true if and only if the application is allowed to submit
203     * command buffers that reference the buffer.
204     *
205     * This can be used to prune BO lists without too many adverse affects on
206     * implicit sync.
207     *
208     * Side note: care needs to be taken for internally delayed submissions wrt
209     * timeline semaphores.
210     */
211    void (*set_memory_ownership)(VkDevice device,
212                                 VkDeviceMemory memory,
213                                 VkBool32 ownership);
214 
215    /*
216     * If this is set, the WSI device will call it to let the driver backend
217     * decide if it can present images directly on the given device fd.
218     */
219    bool (*can_present_on_device)(VkPhysicalDevice pdevice, int fd);
220 
221    /*
222     * A driver can implement this callback to return a special queue to execute
223     * buffer blits.
224     */
225    VkQueue (*get_blit_queue)(VkDevice device);
226 
227 #define WSI_CB(cb) PFN_vk##cb cb
228    WSI_CB(AllocateMemory);
229    WSI_CB(AllocateCommandBuffers);
230    WSI_CB(BindBufferMemory);
231    WSI_CB(BindImageMemory);
232    WSI_CB(BeginCommandBuffer);
233    WSI_CB(CmdPipelineBarrier);
234    WSI_CB(CmdCopyImage);
235    WSI_CB(CmdCopyImageToBuffer);
236    WSI_CB(CreateBuffer);
237    WSI_CB(CreateCommandPool);
238    WSI_CB(CreateFence);
239    WSI_CB(CreateImage);
240    WSI_CB(CreateSemaphore);
241    WSI_CB(DestroyBuffer);
242    WSI_CB(DestroyCommandPool);
243    WSI_CB(DestroyFence);
244    WSI_CB(DestroyImage);
245    WSI_CB(DestroySemaphore);
246    WSI_CB(EndCommandBuffer);
247    WSI_CB(FreeMemory);
248    WSI_CB(FreeCommandBuffers);
249    WSI_CB(GetBufferMemoryRequirements);
250    WSI_CB(GetFenceStatus);
251    WSI_CB(GetImageDrmFormatModifierPropertiesEXT);
252    WSI_CB(GetImageMemoryRequirements);
253    WSI_CB(GetImageSubresourceLayout);
254    WSI_CB(GetMemoryFdKHR);
255    WSI_CB(GetPhysicalDeviceFormatProperties);
256    WSI_CB(GetPhysicalDeviceFormatProperties2KHR);
257    WSI_CB(GetPhysicalDeviceImageFormatProperties2);
258    WSI_CB(GetSemaphoreFdKHR);
259    WSI_CB(ResetFences);
260    WSI_CB(QueueSubmit);
261    WSI_CB(WaitForFences);
262    WSI_CB(MapMemory);
263    WSI_CB(UnmapMemory);
264    WSI_CB(WaitSemaphoresKHR);
265 #undef WSI_CB
266 
267     struct wsi_interface *                  wsi[VK_ICD_WSI_PLATFORM_MAX];
268 };
269 
270 typedef PFN_vkVoidFunction (VKAPI_PTR *WSI_FN_GetPhysicalDeviceProcAddr)(VkPhysicalDevice physicalDevice, const char* pName);
271 
272 struct wsi_device_options {
273    bool sw_device;
274    bool extra_xwayland_image;
275 };
276 
277 VkResult
278 wsi_device_init(struct wsi_device *wsi,
279                 VkPhysicalDevice pdevice,
280                 WSI_FN_GetPhysicalDeviceProcAddr proc_addr,
281                 const VkAllocationCallbacks *alloc,
282                 int display_fd,
283                 const struct driOptionCache *dri_options,
284                 const struct wsi_device_options *device_options);
285 
286 void
287 wsi_device_finish(struct wsi_device *wsi,
288                   const VkAllocationCallbacks *alloc);
289 
290 /* Setup file descriptor to be used with imported sync_fd's in wsi fences. */
291 void
292 wsi_device_setup_syncobj_fd(struct wsi_device *wsi_device,
293                             int fd);
294 
295 #define ICD_DEFINE_NONDISP_HANDLE_CASTS(__VkIcdType, __VkType)             \
296                                                                            \
297    static inline __VkIcdType *                                             \
298    __VkIcdType ## _from_handle(__VkType _handle)                           \
299    {                                                                       \
300       return (__VkIcdType *)(uintptr_t) _handle;                           \
301    }                                                                       \
302                                                                            \
303    static inline __VkType                                                  \
304    __VkIcdType ## _to_handle(__VkIcdType *_obj)                            \
305    {                                                                       \
306       return (__VkType)(uintptr_t) _obj;                                   \
307    }
308 
309 #define ICD_FROM_HANDLE(__VkIcdType, __name, __handle) \
310    __VkIcdType *__name = __VkIcdType ## _from_handle(__handle)
311 
312 ICD_DEFINE_NONDISP_HANDLE_CASTS(VkIcdSurfaceBase, VkSurfaceKHR)
313 
314 VkResult
315 wsi_common_get_images(VkSwapchainKHR _swapchain,
316                       uint32_t *pSwapchainImageCount,
317                       VkImage *pSwapchainImages);
318 
319 VkImage
320 wsi_common_get_image(VkSwapchainKHR _swapchain, uint32_t index);
321 
322 VkResult
323 wsi_common_acquire_next_image2(const struct wsi_device *wsi,
324                                VkDevice device,
325                                const VkAcquireNextImageInfoKHR *pAcquireInfo,
326                                uint32_t *pImageIndex);
327 
328 VkResult
329 wsi_common_queue_present(const struct wsi_device *wsi,
330                          VkDevice device_h,
331                          VkQueue queue_h,
332                          int queue_family_index,
333                          const VkPresentInfoKHR *pPresentInfo);
334 
335 VkResult
336 wsi_common_create_swapchain_image(const struct wsi_device *wsi,
337                                   const VkImageCreateInfo *pCreateInfo,
338                                   VkSwapchainKHR _swapchain,
339                                   VkImage *pImage);
340 VkResult
341 wsi_common_bind_swapchain_image(const struct wsi_device *wsi,
342                                 VkImage vk_image,
343                                 VkSwapchainKHR _swapchain,
344                                 uint32_t image_idx);
345 
346 bool
347 wsi_common_vk_instance_supports_present_wait(const struct vk_instance *instance);
348 
349 #define wsi_common_vk_warn_once(warning) \
350    do { \
351       static int warned = false; \
352       if (!warned) { \
353          mesa_loge(warning); \
354          warned = true; \
355       } \
356    } while (0)
357 
358 #ifdef __cplusplus
359 }
360 #endif
361 
362 #endif
363