• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2020 Raspberry Pi Ltd
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 "wsi_common_entrypoints.h"
29 #include "vk_util.h"
30 #include "wsi_common.h"
31 #include "wsi_common_drm.h"
32 
33 static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
v3dv_wsi_proc_addr(VkPhysicalDevice physicalDevice,const char * pName)34 v3dv_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
35 {
36    V3DV_FROM_HANDLE(v3dv_physical_device, pdevice, physicalDevice);
37    return vk_instance_get_proc_addr_unchecked(pdevice->vk.instance, pName);
38 }
39 
40 static bool
v3dv_wsi_can_present_on_device(VkPhysicalDevice _pdevice,int fd)41 v3dv_wsi_can_present_on_device(VkPhysicalDevice _pdevice, int fd)
42 {
43    V3DV_FROM_HANDLE(v3dv_physical_device, pdevice, _pdevice);
44 
45    return wsi_common_drm_devices_equal(fd, pdevice->display_fd);
46 }
47 
48 VkResult
v3dv_wsi_init(struct v3dv_physical_device * physical_device)49 v3dv_wsi_init(struct v3dv_physical_device *physical_device)
50 {
51    VkResult result;
52 
53    result = wsi_device_init(&physical_device->wsi_device,
54                             v3dv_physical_device_to_handle(physical_device),
55                             v3dv_wsi_proc_addr,
56                             &physical_device->vk.instance->alloc,
57                             physical_device->master_fd, NULL, false);
58 
59    if (result != VK_SUCCESS)
60       return result;
61 
62    physical_device->wsi_device.supports_modifiers = true;
63    physical_device->wsi_device.can_present_on_device =
64       v3dv_wsi_can_present_on_device;
65 
66    physical_device->vk.wsi_device = &physical_device->wsi_device;
67 
68    return VK_SUCCESS;
69 }
70 
71 void
v3dv_wsi_finish(struct v3dv_physical_device * physical_device)72 v3dv_wsi_finish(struct v3dv_physical_device *physical_device)
73 {
74    physical_device->vk.wsi_device = NULL;
75    wsi_device_finish(&physical_device->wsi_device,
76                      &physical_device->vk.instance->alloc);
77 }
78 
79 static void
constraint_surface_capabilities(VkSurfaceCapabilitiesKHR * caps)80 constraint_surface_capabilities(VkSurfaceCapabilitiesKHR *caps)
81 {
82    /* Our display pipeline requires that images are linear, so we cannot
83     * ensure that our swapchain images can be sampled. If we are running under
84     * a compositor in windowed mode, the DRM modifier negotiation should
85     * probably end up selecting an UIF layout for the swapchain images but it
86     * may still choose linear and send images directly for scanout if the
87     * surface is in fullscreen mode for example. If we are not running under
88     * a compositor, then we would always need them to be linear anyway.
89     */
90    caps->supportedUsageFlags &= ~VK_IMAGE_USAGE_SAMPLED_BIT;
91 }
92 
93 VKAPI_ATTR VkResult VKAPI_CALL
v3dv_GetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice physicalDevice,VkSurfaceKHR surface,VkSurfaceCapabilitiesKHR * pSurfaceCapabilities)94 v3dv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
95     VkPhysicalDevice                            physicalDevice,
96     VkSurfaceKHR                                surface,
97     VkSurfaceCapabilitiesKHR*                   pSurfaceCapabilities)
98 {
99    VkResult result;
100    result = wsi_GetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice,
101                                                         surface,
102                                                         pSurfaceCapabilities);
103    constraint_surface_capabilities(pSurfaceCapabilities);
104    return result;
105 }
106 
107 VKAPI_ATTR VkResult VKAPI_CALL
v3dv_GetPhysicalDeviceSurfaceCapabilities2KHR(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSurfaceInfo2KHR * pSurfaceInfo,VkSurfaceCapabilities2KHR * pSurfaceCapabilities)108 v3dv_GetPhysicalDeviceSurfaceCapabilities2KHR(
109     VkPhysicalDevice                            physicalDevice,
110     const VkPhysicalDeviceSurfaceInfo2KHR*      pSurfaceInfo,
111     VkSurfaceCapabilities2KHR*                  pSurfaceCapabilities)
112 {
113    VkResult result;
114    result = wsi_GetPhysicalDeviceSurfaceCapabilities2KHR(physicalDevice,
115                                                          pSurfaceInfo,
116                                                          pSurfaceCapabilities);
117    constraint_surface_capabilities(&pSurfaceCapabilities->surfaceCapabilities);
118    return result;
119 }
120 
121 VKAPI_ATTR VkResult VKAPI_CALL
v3dv_CreateSwapchainKHR(VkDevice _device,const VkSwapchainCreateInfoKHR * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkSwapchainKHR * pSwapchain)122 v3dv_CreateSwapchainKHR(
123     VkDevice                                     _device,
124     const VkSwapchainCreateInfoKHR*              pCreateInfo,
125     const VkAllocationCallbacks*                 pAllocator,
126     VkSwapchainKHR*                              pSwapchain)
127 {
128    V3DV_FROM_HANDLE(v3dv_device, device, _device);
129    struct v3dv_instance *instance = device->instance;
130    struct v3dv_physical_device *pdevice = &instance->physicalDevice;
131 
132    ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, pCreateInfo->surface);
133    VkResult result =
134       v3dv_physical_device_acquire_display(instance, pdevice, surface);
135    if (result != VK_SUCCESS)
136       return result;
137 
138    return wsi_CreateSwapchainKHR(_device, pCreateInfo, pAllocator, pSwapchain);
139 }
140 
141 struct v3dv_image *
v3dv_wsi_get_image_from_swapchain(VkSwapchainKHR swapchain,uint32_t index)142 v3dv_wsi_get_image_from_swapchain(VkSwapchainKHR swapchain, uint32_t index)
143 {
144    VkImage image = wsi_common_get_image(swapchain, index);
145    return v3dv_image_from_handle(image);
146 }
147