1/*
2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26#include "util/macros.h"
27#include "radv_meta.h"
28#include "radv_private.h"
29#include "vk_util.h"
30#include "wsi_common.h"
31
32static PFN_vkVoidFunction
33radv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
34{
35   RADV_FROM_HANDLE(radv_physical_device, pdevice, physicalDevice);
36   return vk_instance_get_proc_addr_unchecked(&pdevice->instance->vk, pName);
37}
38
39static void
40radv_wsi_set_memory_ownership(VkDevice _device, VkDeviceMemory _mem, VkBool32 ownership)
41{
42   RADV_FROM_HANDLE(radv_device, device, _device);
43   RADV_FROM_HANDLE(radv_device_memory, mem, _mem);
44
45   if (device->use_global_bo_list) {
46      device->ws->buffer_make_resident(device->ws, mem->bo, ownership);
47   }
48}
49
50VkResult
51radv_init_wsi(struct radv_physical_device *physical_device)
52{
53   VkResult result =
54      wsi_device_init(&physical_device->wsi_device, radv_physical_device_to_handle(physical_device),
55                      radv_wsi_proc_addr, &physical_device->instance->vk.alloc,
56                      physical_device->master_fd, &physical_device->instance->dri_options, false);
57   if (result != VK_SUCCESS)
58      return result;
59
60   physical_device->wsi_device.supports_modifiers = physical_device->rad_info.chip_class >= GFX9;
61   physical_device->wsi_device.set_memory_ownership = radv_wsi_set_memory_ownership;
62
63   physical_device->vk.wsi_device = &physical_device->wsi_device;
64
65   return VK_SUCCESS;
66}
67
68void
69radv_finish_wsi(struct radv_physical_device *physical_device)
70{
71   physical_device->vk.wsi_device = NULL;
72   wsi_device_finish(&physical_device->wsi_device, &physical_device->instance->vk.alloc);
73}
74
75VkResult
76radv_AcquireNextImage2KHR(VkDevice _device, const VkAcquireNextImageInfoKHR *pAcquireInfo,
77                          uint32_t *pImageIndex)
78{
79   RADV_FROM_HANDLE(radv_device, device, _device);
80   struct radv_physical_device *pdevice = device->physical_device;
81   RADV_FROM_HANDLE(radv_fence, fence, pAcquireInfo->fence);
82   RADV_FROM_HANDLE(radv_semaphore, semaphore, pAcquireInfo->semaphore);
83
84   VkResult result =
85      wsi_common_acquire_next_image2(&pdevice->wsi_device, _device, pAcquireInfo, pImageIndex);
86
87   if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) {
88      if (fence) {
89         struct radv_fence_part *part =
90            fence->temporary.kind != RADV_FENCE_NONE ? &fence->temporary : &fence->permanent;
91
92         device->ws->signal_syncobj(device->ws, part->syncobj, 0);
93      }
94      if (semaphore) {
95         struct radv_semaphore_part *part = semaphore->temporary.kind != RADV_SEMAPHORE_NONE
96                                               ? &semaphore->temporary
97                                               : &semaphore->permanent;
98
99         switch (part->kind) {
100         case RADV_SEMAPHORE_NONE:
101            /* Do not need to do anything. */
102            break;
103         case RADV_SEMAPHORE_TIMELINE:
104         case RADV_SEMAPHORE_TIMELINE_SYNCOBJ:
105            unreachable("WSI only allows binary semaphores.");
106         case RADV_SEMAPHORE_SYNCOBJ:
107            device->ws->signal_syncobj(device->ws, part->syncobj, 0);
108            break;
109         }
110      }
111   }
112   return result;
113}
114
115VkResult
116radv_QueuePresentKHR(VkQueue _queue, const VkPresentInfoKHR *pPresentInfo)
117{
118   RADV_FROM_HANDLE(radv_queue, queue, _queue);
119   return wsi_common_queue_present(&queue->device->physical_device->wsi_device,
120                                   radv_device_to_handle(queue->device), _queue,
121                                   queue->vk.queue_family_index, pPresentInfo);
122}
123