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