Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/broadcom/vulkan/v3dv_wsi.c
4560 views
1
/*
2
* Copyright © 2020 Raspberry Pi
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 "v3dv_private.h"
27
#include "drm-uapi/drm_fourcc.h"
28
#include "vk_format_info.h"
29
#include "vk_util.h"
30
#include "wsi_common.h"
31
32
static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
33
v3dv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
34
{
35
V3DV_FROM_HANDLE(v3dv_physical_device, pdevice, physicalDevice);
36
PFN_vkVoidFunction func;
37
38
func = vk_instance_dispatch_table_get(&pdevice->vk.instance->dispatch_table, pName);
39
if (func != NULL)
40
return func;
41
42
func = vk_physical_device_dispatch_table_get(&pdevice->vk.dispatch_table, pName);
43
if (func != NULL)
44
return func;
45
46
return vk_device_dispatch_table_get(&vk_device_trampolines, pName);
47
}
48
49
static bool
50
v3dv_wsi_can_present_on_device(VkPhysicalDevice _pdevice, int fd)
51
{
52
V3DV_FROM_HANDLE(v3dv_physical_device, pdevice, _pdevice);
53
54
drmDevicePtr fd_devinfo, display_devinfo;
55
int ret;
56
57
ret = drmGetDevice2(fd, 0, &fd_devinfo);
58
if (ret)
59
return false;
60
61
ret = drmGetDevice2(pdevice->display_fd, 0, &display_devinfo);
62
if (ret) {
63
drmFreeDevice(&fd_devinfo);
64
return false;
65
}
66
67
bool result = drmDevicesEqual(fd_devinfo, display_devinfo);
68
69
drmFreeDevice(&fd_devinfo);
70
drmFreeDevice(&display_devinfo);
71
return result;
72
}
73
74
VkResult
75
v3dv_wsi_init(struct v3dv_physical_device *physical_device)
76
{
77
VkResult result;
78
79
result = wsi_device_init(&physical_device->wsi_device,
80
v3dv_physical_device_to_handle(physical_device),
81
v3dv_wsi_proc_addr,
82
&physical_device->vk.instance->alloc,
83
physical_device->master_fd, NULL, false);
84
85
if (result != VK_SUCCESS)
86
return result;
87
88
physical_device->wsi_device.supports_modifiers = true;
89
physical_device->wsi_device.can_present_on_device =
90
v3dv_wsi_can_present_on_device;
91
92
return VK_SUCCESS;
93
}
94
95
void
96
v3dv_wsi_finish(struct v3dv_physical_device *physical_device)
97
{
98
wsi_device_finish(&physical_device->wsi_device,
99
&physical_device->vk.instance->alloc);
100
}
101
102
VKAPI_ATTR void VKAPI_CALL
103
v3dv_DestroySurfaceKHR(
104
VkInstance _instance,
105
VkSurfaceKHR _surface,
106
const VkAllocationCallbacks* pAllocator)
107
{
108
V3DV_FROM_HANDLE(v3dv_instance, instance, _instance);
109
ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
110
111
if (!surface)
112
return;
113
114
vk_free2(&instance->vk.alloc, pAllocator, surface);
115
}
116
117
VKAPI_ATTR VkResult VKAPI_CALL
118
v3dv_GetPhysicalDeviceSurfaceSupportKHR(
119
VkPhysicalDevice physicalDevice,
120
uint32_t queueFamilyIndex,
121
VkSurfaceKHR surface,
122
VkBool32* pSupported)
123
{
124
V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
125
126
return wsi_common_get_surface_support(&device->wsi_device,
127
queueFamilyIndex,
128
surface,
129
pSupported);
130
}
131
132
static void
133
constraint_surface_capabilities(VkSurfaceCapabilitiesKHR *caps)
134
{
135
/* Our display pipeline requires that images are linear, so we cannot
136
* ensure that our swapchain images can be sampled. If we are running under
137
* a compositor in windowed mode, the DRM modifier negotiation should
138
* probably end up selecting an UIF layout for the swapchain images but it
139
* may still choose linear and send images directly for scanout if the
140
* surface is in fullscreen mode for example. If we are not running under
141
* a compositor, then we would always need them to be linear anyway.
142
*/
143
caps->supportedUsageFlags &= ~VK_IMAGE_USAGE_SAMPLED_BIT;
144
}
145
146
VKAPI_ATTR VkResult VKAPI_CALL
147
v3dv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
148
VkPhysicalDevice physicalDevice,
149
VkSurfaceKHR surface,
150
VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
151
{
152
V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
153
154
VkResult result;
155
result = wsi_common_get_surface_capabilities(&device->wsi_device,
156
surface,
157
pSurfaceCapabilities);
158
constraint_surface_capabilities(pSurfaceCapabilities);
159
return result;
160
}
161
162
VKAPI_ATTR VkResult VKAPI_CALL
163
v3dv_GetPhysicalDeviceSurfaceCapabilities2KHR(
164
VkPhysicalDevice physicalDevice,
165
const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
166
VkSurfaceCapabilities2KHR* pSurfaceCapabilities)
167
{
168
V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
169
170
VkResult result;
171
result = wsi_common_get_surface_capabilities2(&device->wsi_device,
172
pSurfaceInfo,
173
pSurfaceCapabilities);
174
constraint_surface_capabilities(&pSurfaceCapabilities->surfaceCapabilities);
175
return result;
176
}
177
178
VKAPI_ATTR VkResult VKAPI_CALL
179
v3dv_GetPhysicalDeviceSurfaceFormatsKHR(
180
VkPhysicalDevice physicalDevice,
181
VkSurfaceKHR surface,
182
uint32_t* pSurfaceFormatCount,
183
VkSurfaceFormatKHR* pSurfaceFormats)
184
{
185
V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
186
187
return wsi_common_get_surface_formats(&device->wsi_device, surface,
188
pSurfaceFormatCount, pSurfaceFormats);
189
}
190
191
VKAPI_ATTR VkResult VKAPI_CALL
192
v3dv_GetPhysicalDeviceSurfaceFormats2KHR(
193
VkPhysicalDevice physicalDevice,
194
const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
195
uint32_t* pSurfaceFormatCount,
196
VkSurfaceFormat2KHR* pSurfaceFormats)
197
{
198
V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
199
200
return wsi_common_get_surface_formats2(&device->wsi_device, pSurfaceInfo,
201
pSurfaceFormatCount, pSurfaceFormats);
202
}
203
204
VKAPI_ATTR VkResult VKAPI_CALL
205
v3dv_GetPhysicalDeviceSurfacePresentModesKHR(
206
VkPhysicalDevice physicalDevice,
207
VkSurfaceKHR surface,
208
uint32_t* pPresentModeCount,
209
VkPresentModeKHR* pPresentModes)
210
{
211
V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
212
213
return wsi_common_get_surface_present_modes(&device->wsi_device, surface,
214
pPresentModeCount,
215
pPresentModes);
216
}
217
218
VKAPI_ATTR VkResult VKAPI_CALL
219
v3dv_CreateSwapchainKHR(
220
VkDevice _device,
221
const VkSwapchainCreateInfoKHR* pCreateInfo,
222
const VkAllocationCallbacks* pAllocator,
223
VkSwapchainKHR* pSwapchain)
224
{
225
V3DV_FROM_HANDLE(v3dv_device, device, _device);
226
struct v3dv_instance *instance = device->instance;
227
struct v3dv_physical_device *pdevice = &instance->physicalDevice;
228
struct wsi_device *wsi_device = &pdevice->wsi_device;
229
230
ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
231
VkResult result =
232
v3dv_physical_device_acquire_display(instance, pdevice, surface);
233
if (result != VK_SUCCESS)
234
return result;
235
236
const VkAllocationCallbacks *alloc;
237
if (pAllocator)
238
alloc = pAllocator;
239
else
240
alloc = &device->vk.alloc;
241
242
return wsi_common_create_swapchain(wsi_device, _device,
243
pCreateInfo, alloc, pSwapchain);
244
}
245
246
VKAPI_ATTR void VKAPI_CALL
247
v3dv_DestroySwapchainKHR(
248
VkDevice _device,
249
VkSwapchainKHR swapchain,
250
const VkAllocationCallbacks* pAllocator)
251
{
252
V3DV_FROM_HANDLE(v3dv_device, device, _device);
253
const VkAllocationCallbacks *alloc;
254
255
if (pAllocator)
256
alloc = pAllocator;
257
else
258
alloc = &device->vk.alloc;
259
260
wsi_common_destroy_swapchain(_device, swapchain, alloc);
261
}
262
263
VKAPI_ATTR VkResult VKAPI_CALL
264
v3dv_GetSwapchainImagesKHR(
265
VkDevice device,
266
VkSwapchainKHR swapchain,
267
uint32_t* pSwapchainImageCount,
268
VkImage* pSwapchainImages)
269
{
270
return wsi_common_get_images(swapchain,
271
pSwapchainImageCount,
272
pSwapchainImages);
273
}
274
275
struct v3dv_image *
276
v3dv_wsi_get_image_from_swapchain(VkSwapchainKHR swapchain, uint32_t index)
277
{
278
uint32_t n_images = index + 1;
279
VkImage *images = malloc(sizeof(*images) * n_images);
280
VkResult result = wsi_common_get_images(swapchain, &n_images, images);
281
282
if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
283
free(images);
284
return NULL;
285
}
286
287
V3DV_FROM_HANDLE(v3dv_image, image, images[index]);
288
free(images);
289
290
return image;
291
}
292
293
VKAPI_ATTR VkResult VKAPI_CALL
294
v3dv_AcquireNextImageKHR(
295
VkDevice device,
296
VkSwapchainKHR swapchain,
297
uint64_t timeout,
298
VkSemaphore semaphore,
299
VkFence fence,
300
uint32_t* pImageIndex)
301
{
302
VkAcquireNextImageInfoKHR acquire_info = {
303
.sType = VK_STRUCTURE_TYPE_ACQUIRE_NEXT_IMAGE_INFO_KHR,
304
.swapchain = swapchain,
305
.timeout = timeout,
306
.semaphore = semaphore,
307
.fence = fence,
308
.deviceMask = 0,
309
};
310
311
return v3dv_AcquireNextImage2KHR(device, &acquire_info, pImageIndex);
312
}
313
314
VKAPI_ATTR VkResult VKAPI_CALL
315
v3dv_AcquireNextImage2KHR(
316
VkDevice _device,
317
const VkAcquireNextImageInfoKHR* pAcquireInfo,
318
uint32_t* pImageIndex)
319
{
320
V3DV_FROM_HANDLE(v3dv_device, device, _device);
321
V3DV_FROM_HANDLE(v3dv_fence, fence, pAcquireInfo->fence);
322
V3DV_FROM_HANDLE(v3dv_semaphore, semaphore, pAcquireInfo->semaphore);
323
324
struct v3dv_physical_device *pdevice = &device->instance->physicalDevice;
325
326
VkResult result;
327
result = wsi_common_acquire_next_image2(&pdevice->wsi_device, _device,
328
pAcquireInfo, pImageIndex);
329
330
if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) {
331
if (fence)
332
drmSyncobjSignal(pdevice->render_fd, &fence->sync, 1);
333
if (semaphore)
334
drmSyncobjSignal(pdevice->render_fd, &semaphore->sync, 1);
335
}
336
337
return result;
338
}
339
340
VKAPI_ATTR VkResult VKAPI_CALL
341
v3dv_QueuePresentKHR(
342
VkQueue _queue,
343
const VkPresentInfoKHR* pPresentInfo)
344
{
345
V3DV_FROM_HANDLE(v3dv_queue, queue, _queue);
346
struct v3dv_physical_device *pdevice =
347
&queue->device->instance->physicalDevice;
348
349
return wsi_common_queue_present(&pdevice->wsi_device,
350
v3dv_device_to_handle(queue->device),
351
_queue, 0,
352
pPresentInfo);
353
}
354
355
VKAPI_ATTR VkResult VKAPI_CALL
356
v3dv_GetDeviceGroupPresentCapabilitiesKHR(
357
VkDevice device,
358
VkDeviceGroupPresentCapabilitiesKHR* pCapabilities)
359
{
360
memset(pCapabilities->presentMask, 0,
361
sizeof(pCapabilities->presentMask));
362
pCapabilities->presentMask[0] = 0x1;
363
pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
364
365
return VK_SUCCESS;
366
}
367
368
VKAPI_ATTR VkResult VKAPI_CALL
369
v3dv_GetDeviceGroupSurfacePresentModesKHR(
370
VkDevice device,
371
VkSurfaceKHR surface,
372
VkDeviceGroupPresentModeFlagsKHR* pModes)
373
{
374
*pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
375
376
return VK_SUCCESS;
377
}
378
379
VKAPI_ATTR VkResult VKAPI_CALL
380
v3dv_GetPhysicalDevicePresentRectanglesKHR(
381
VkPhysicalDevice physicalDevice,
382
VkSurfaceKHR surface,
383
uint32_t* pRectCount,
384
VkRect2D* pRects)
385
{
386
V3DV_FROM_HANDLE(v3dv_physical_device, device, physicalDevice);
387
388
return wsi_common_get_present_rectangles(&device->wsi_device,
389
surface,
390
pRectCount, pRects);
391
}
392
393