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