17ec681f3Smrg/* 27ec681f3Smrg * Copyright © 2017 Intel Corporation 37ec681f3Smrg * 47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 57ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 67ec681f3Smrg * to deal in the Software without restriction, including without limitation 77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 97ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 107ec681f3Smrg * 117ec681f3Smrg * The above copyright notice and this permission notice (including the next 127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 137ec681f3Smrg * Software. 147ec681f3Smrg * 157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 217ec681f3Smrg * IN THE SOFTWARE. 227ec681f3Smrg */ 237ec681f3Smrg 247ec681f3Smrg#include "wsi_common_private.h" 257ec681f3Smrg#include "util/macros.h" 267ec681f3Smrg#include "util/os_file.h" 277ec681f3Smrg#include "util/xmlconfig.h" 287ec681f3Smrg#include "vk_util.h" 297ec681f3Smrg#include "drm-uapi/drm_fourcc.h" 307ec681f3Smrg 317ec681f3Smrg#include <time.h> 327ec681f3Smrg#include <unistd.h> 337ec681f3Smrg#include <stdlib.h> 347ec681f3Smrg#include <stdio.h> 357ec681f3Smrg#include <xf86drm.h> 367ec681f3Smrg 377ec681f3Smrgbool 387ec681f3Smrgwsi_device_matches_drm_fd(const struct wsi_device *wsi, int drm_fd) 397ec681f3Smrg{ 407ec681f3Smrg if (wsi->can_present_on_device) 417ec681f3Smrg return wsi->can_present_on_device(wsi->pdevice, drm_fd); 427ec681f3Smrg 437ec681f3Smrg drmDevicePtr fd_device; 447ec681f3Smrg int ret = drmGetDevice2(drm_fd, 0, &fd_device); 457ec681f3Smrg if (ret) 467ec681f3Smrg return false; 477ec681f3Smrg 487ec681f3Smrg bool match = false; 497ec681f3Smrg switch (fd_device->bustype) { 507ec681f3Smrg case DRM_BUS_PCI: 517ec681f3Smrg match = wsi->pci_bus_info.pciDomain == fd_device->businfo.pci->domain && 527ec681f3Smrg wsi->pci_bus_info.pciBus == fd_device->businfo.pci->bus && 537ec681f3Smrg wsi->pci_bus_info.pciDevice == fd_device->businfo.pci->dev && 547ec681f3Smrg wsi->pci_bus_info.pciFunction == fd_device->businfo.pci->func; 557ec681f3Smrg break; 567ec681f3Smrg 577ec681f3Smrg default: 587ec681f3Smrg break; 597ec681f3Smrg } 607ec681f3Smrg 617ec681f3Smrg drmFreeDevice(&fd_device); 627ec681f3Smrg 637ec681f3Smrg return match; 647ec681f3Smrg} 657ec681f3Smrg 667ec681f3Smrgstatic uint32_t 677ec681f3Smrgselect_memory_type(const struct wsi_device *wsi, 687ec681f3Smrg bool want_device_local, 697ec681f3Smrg uint32_t type_bits) 707ec681f3Smrg{ 717ec681f3Smrg assert(type_bits); 727ec681f3Smrg 737ec681f3Smrg bool all_local = true; 747ec681f3Smrg for (uint32_t i = 0; i < wsi->memory_props.memoryTypeCount; i++) { 757ec681f3Smrg const VkMemoryType type = wsi->memory_props.memoryTypes[i]; 767ec681f3Smrg bool local = type.propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; 777ec681f3Smrg 787ec681f3Smrg if ((type_bits & (1 << i)) && local == want_device_local) 797ec681f3Smrg return i; 807ec681f3Smrg all_local &= local; 817ec681f3Smrg } 827ec681f3Smrg 837ec681f3Smrg /* ignore want_device_local when all memory types are device-local */ 847ec681f3Smrg if (all_local) { 857ec681f3Smrg assert(!want_device_local); 867ec681f3Smrg return ffs(type_bits) - 1; 877ec681f3Smrg } 887ec681f3Smrg 897ec681f3Smrg unreachable("No memory type found"); 907ec681f3Smrg} 917ec681f3Smrg 927ec681f3Smrgstatic uint32_t 937ec681f3Smrgvk_format_size(VkFormat format) 947ec681f3Smrg{ 957ec681f3Smrg switch (format) { 967ec681f3Smrg case VK_FORMAT_B8G8R8A8_UNORM: 977ec681f3Smrg case VK_FORMAT_B8G8R8A8_SRGB: 987ec681f3Smrg return 4; 997ec681f3Smrg default: 1007ec681f3Smrg unreachable("Unknown WSI Format"); 1017ec681f3Smrg } 1027ec681f3Smrg} 1037ec681f3Smrg 1047ec681f3SmrgVkResult 1057ec681f3Smrgwsi_create_native_image(const struct wsi_swapchain *chain, 1067ec681f3Smrg const VkSwapchainCreateInfoKHR *pCreateInfo, 1077ec681f3Smrg uint32_t num_modifier_lists, 1087ec681f3Smrg const uint32_t *num_modifiers, 1097ec681f3Smrg const uint64_t *const *modifiers, 1107ec681f3Smrg uint8_t *(alloc_shm)(struct wsi_image *image, unsigned size), 1117ec681f3Smrg struct wsi_image *image) 1127ec681f3Smrg{ 1137ec681f3Smrg const struct wsi_device *wsi = chain->wsi; 1147ec681f3Smrg VkResult result; 1157ec681f3Smrg 1167ec681f3Smrg memset(image, 0, sizeof(*image)); 1177ec681f3Smrg for (int i = 0; i < ARRAY_SIZE(image->fds); i++) 1187ec681f3Smrg image->fds[i] = -1; 1197ec681f3Smrg 1207ec681f3Smrg struct wsi_image_create_info image_wsi_info = { 1217ec681f3Smrg .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA, 1227ec681f3Smrg }; 1237ec681f3Smrg VkExternalMemoryImageCreateInfo ext_mem_image_create_info = { 1247ec681f3Smrg .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, 1257ec681f3Smrg .pNext = &image_wsi_info, 1267ec681f3Smrg .handleTypes = wsi->sw ? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT : 1277ec681f3Smrg VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, 1287ec681f3Smrg }; 1297ec681f3Smrg VkImageCreateInfo image_info = { 1307ec681f3Smrg .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, 1317ec681f3Smrg .pNext = &ext_mem_image_create_info, 1327ec681f3Smrg .flags = 0, 1337ec681f3Smrg .imageType = VK_IMAGE_TYPE_2D, 1347ec681f3Smrg .format = pCreateInfo->imageFormat, 1357ec681f3Smrg .extent = { 1367ec681f3Smrg .width = pCreateInfo->imageExtent.width, 1377ec681f3Smrg .height = pCreateInfo->imageExtent.height, 1387ec681f3Smrg .depth = 1, 1397ec681f3Smrg }, 1407ec681f3Smrg .mipLevels = 1, 1417ec681f3Smrg .arrayLayers = 1, 1427ec681f3Smrg .samples = VK_SAMPLE_COUNT_1_BIT, 1437ec681f3Smrg .tiling = VK_IMAGE_TILING_OPTIMAL, 1447ec681f3Smrg .usage = pCreateInfo->imageUsage, 1457ec681f3Smrg .sharingMode = pCreateInfo->imageSharingMode, 1467ec681f3Smrg .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount, 1477ec681f3Smrg .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices, 1487ec681f3Smrg .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, 1497ec681f3Smrg }; 1507ec681f3Smrg 1517ec681f3Smrg VkImageFormatListCreateInfoKHR image_format_list; 1527ec681f3Smrg if (pCreateInfo->flags & VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR) { 1537ec681f3Smrg image_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | 1547ec681f3Smrg VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR; 1557ec681f3Smrg 1567ec681f3Smrg const VkImageFormatListCreateInfoKHR *format_list = 1577ec681f3Smrg vk_find_struct_const(pCreateInfo->pNext, 1587ec681f3Smrg IMAGE_FORMAT_LIST_CREATE_INFO_KHR); 1597ec681f3Smrg 1607ec681f3Smrg#ifndef NDEBUG 1617ec681f3Smrg assume(format_list && format_list->viewFormatCount > 0); 1627ec681f3Smrg bool format_found = false; 1637ec681f3Smrg for (int i = 0; i < format_list->viewFormatCount; i++) 1647ec681f3Smrg if (pCreateInfo->imageFormat == format_list->pViewFormats[i]) 1657ec681f3Smrg format_found = true; 1667ec681f3Smrg assert(format_found); 1677ec681f3Smrg#endif 1687ec681f3Smrg 1697ec681f3Smrg image_format_list = *format_list; 1707ec681f3Smrg image_format_list.pNext = NULL; 1717ec681f3Smrg __vk_append_struct(&image_info, &image_format_list); 1727ec681f3Smrg } 1737ec681f3Smrg 1747ec681f3Smrg VkImageDrmFormatModifierListCreateInfoEXT image_modifier_list; 1757ec681f3Smrg 1767ec681f3Smrg uint32_t image_modifier_count = 0, modifier_prop_count = 0; 1777ec681f3Smrg struct VkDrmFormatModifierPropertiesEXT *modifier_props = NULL; 1787ec681f3Smrg uint64_t *image_modifiers = NULL; 1797ec681f3Smrg if (num_modifier_lists == 0) { 1807ec681f3Smrg /* If we don't have modifiers, fall back to the legacy "scanout" flag */ 1817ec681f3Smrg image_wsi_info.scanout = true; 1827ec681f3Smrg } else { 1837ec681f3Smrg /* The winsys can't request modifiers if we don't support them. */ 1847ec681f3Smrg assert(wsi->supports_modifiers); 1857ec681f3Smrg struct VkDrmFormatModifierPropertiesListEXT modifier_props_list = { 1867ec681f3Smrg .sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT, 1877ec681f3Smrg }; 1887ec681f3Smrg VkFormatProperties2 format_props = { 1897ec681f3Smrg .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, 1907ec681f3Smrg .pNext = &modifier_props_list, 1917ec681f3Smrg }; 1927ec681f3Smrg wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice, 1937ec681f3Smrg pCreateInfo->imageFormat, 1947ec681f3Smrg &format_props); 1957ec681f3Smrg assert(modifier_props_list.drmFormatModifierCount > 0); 1967ec681f3Smrg modifier_props = vk_alloc(&chain->alloc, 1977ec681f3Smrg sizeof(*modifier_props) * 1987ec681f3Smrg modifier_props_list.drmFormatModifierCount, 1997ec681f3Smrg 8, 2007ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_COMMAND); 2017ec681f3Smrg if (!modifier_props) { 2027ec681f3Smrg result = VK_ERROR_OUT_OF_HOST_MEMORY; 2037ec681f3Smrg goto fail; 2047ec681f3Smrg } 2057ec681f3Smrg 2067ec681f3Smrg modifier_props_list.pDrmFormatModifierProperties = modifier_props; 2077ec681f3Smrg wsi->GetPhysicalDeviceFormatProperties2KHR(wsi->pdevice, 2087ec681f3Smrg pCreateInfo->imageFormat, 2097ec681f3Smrg &format_props); 2107ec681f3Smrg 2117ec681f3Smrg /* Call GetImageFormatProperties with every modifier and filter the list 2127ec681f3Smrg * down to those that we know work. 2137ec681f3Smrg */ 2147ec681f3Smrg modifier_prop_count = 0; 2157ec681f3Smrg for (uint32_t i = 0; i < modifier_props_list.drmFormatModifierCount; i++) { 2167ec681f3Smrg VkPhysicalDeviceImageDrmFormatModifierInfoEXT mod_info = { 2177ec681f3Smrg .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT, 2187ec681f3Smrg .drmFormatModifier = modifier_props[i].drmFormatModifier, 2197ec681f3Smrg .sharingMode = pCreateInfo->imageSharingMode, 2207ec681f3Smrg .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount, 2217ec681f3Smrg .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices, 2227ec681f3Smrg }; 2237ec681f3Smrg VkPhysicalDeviceImageFormatInfo2 format_info = { 2247ec681f3Smrg .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2, 2257ec681f3Smrg .format = pCreateInfo->imageFormat, 2267ec681f3Smrg .type = VK_IMAGE_TYPE_2D, 2277ec681f3Smrg .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT, 2287ec681f3Smrg .usage = pCreateInfo->imageUsage, 2297ec681f3Smrg .flags = image_info.flags, 2307ec681f3Smrg }; 2317ec681f3Smrg 2327ec681f3Smrg VkImageFormatListCreateInfoKHR format_list; 2337ec681f3Smrg if (image_info.flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) { 2347ec681f3Smrg format_list = image_format_list; 2357ec681f3Smrg format_list.pNext = NULL; 2367ec681f3Smrg __vk_append_struct(&format_info, &format_list); 2377ec681f3Smrg } 2387ec681f3Smrg 2397ec681f3Smrg VkImageFormatProperties2 format_props = { 2407ec681f3Smrg .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2, 2417ec681f3Smrg .pNext = NULL, 2427ec681f3Smrg }; 2437ec681f3Smrg __vk_append_struct(&format_info, &mod_info); 2447ec681f3Smrg result = wsi->GetPhysicalDeviceImageFormatProperties2(wsi->pdevice, 2457ec681f3Smrg &format_info, 2467ec681f3Smrg &format_props); 2477ec681f3Smrg if (result == VK_SUCCESS) 2487ec681f3Smrg modifier_props[modifier_prop_count++] = modifier_props[i]; 2497ec681f3Smrg } 2507ec681f3Smrg 2517ec681f3Smrg uint32_t max_modifier_count = 0; 2527ec681f3Smrg for (uint32_t l = 0; l < num_modifier_lists; l++) 2537ec681f3Smrg max_modifier_count = MAX2(max_modifier_count, num_modifiers[l]); 2547ec681f3Smrg 2557ec681f3Smrg image_modifiers = vk_alloc(&chain->alloc, 2567ec681f3Smrg sizeof(*image_modifiers) * 2577ec681f3Smrg max_modifier_count, 2587ec681f3Smrg 8, 2597ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_COMMAND); 2607ec681f3Smrg if (!image_modifiers) { 2617ec681f3Smrg result = VK_ERROR_OUT_OF_HOST_MEMORY; 2627ec681f3Smrg goto fail; 2637ec681f3Smrg } 2647ec681f3Smrg 2657ec681f3Smrg image_modifier_count = 0; 2667ec681f3Smrg for (uint32_t l = 0; l < num_modifier_lists; l++) { 2677ec681f3Smrg /* Walk the modifier lists and construct a list of supported 2687ec681f3Smrg * modifiers. 2697ec681f3Smrg */ 2707ec681f3Smrg for (uint32_t i = 0; i < num_modifiers[l]; i++) { 2717ec681f3Smrg for (uint32_t j = 0; j < modifier_prop_count; j++) { 2727ec681f3Smrg if (modifier_props[j].drmFormatModifier == modifiers[l][i]) 2737ec681f3Smrg image_modifiers[image_modifier_count++] = modifiers[l][i]; 2747ec681f3Smrg } 2757ec681f3Smrg } 2767ec681f3Smrg 2777ec681f3Smrg /* We only want to take the modifiers from the first list */ 2787ec681f3Smrg if (image_modifier_count > 0) 2797ec681f3Smrg break; 2807ec681f3Smrg } 2817ec681f3Smrg 2827ec681f3Smrg if (image_modifier_count > 0) { 2837ec681f3Smrg image_modifier_list = (VkImageDrmFormatModifierListCreateInfoEXT) { 2847ec681f3Smrg .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT, 2857ec681f3Smrg .drmFormatModifierCount = image_modifier_count, 2867ec681f3Smrg .pDrmFormatModifiers = image_modifiers, 2877ec681f3Smrg }; 2887ec681f3Smrg image_info.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT; 2897ec681f3Smrg __vk_append_struct(&image_info, &image_modifier_list); 2907ec681f3Smrg } else { 2917ec681f3Smrg /* TODO: Add a proper error here */ 2927ec681f3Smrg assert(!"Failed to find a supported modifier! This should never " 2937ec681f3Smrg "happen because LINEAR should always be available"); 2947ec681f3Smrg result = VK_ERROR_OUT_OF_HOST_MEMORY; 2957ec681f3Smrg goto fail; 2967ec681f3Smrg } 2977ec681f3Smrg } 2987ec681f3Smrg 2997ec681f3Smrg result = wsi->CreateImage(chain->device, &image_info, 3007ec681f3Smrg &chain->alloc, &image->image); 3017ec681f3Smrg if (result != VK_SUCCESS) 3027ec681f3Smrg goto fail; 3037ec681f3Smrg 3047ec681f3Smrg VkMemoryRequirements reqs; 3057ec681f3Smrg wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs); 3067ec681f3Smrg 3077ec681f3Smrg void *sw_host_ptr = NULL; 3087ec681f3Smrg if (alloc_shm) { 3097ec681f3Smrg VkSubresourceLayout layout; 3107ec681f3Smrg 3117ec681f3Smrg wsi->GetImageSubresourceLayout(chain->device, image->image, 3127ec681f3Smrg &(VkImageSubresource) { 3137ec681f3Smrg .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, 3147ec681f3Smrg .mipLevel = 0, 3157ec681f3Smrg .arrayLayer = 0, 3167ec681f3Smrg }, &layout); 3177ec681f3Smrg sw_host_ptr = (*alloc_shm)(image, layout.size); 3187ec681f3Smrg } 3197ec681f3Smrg 3207ec681f3Smrg const struct wsi_memory_allocate_info memory_wsi_info = { 3217ec681f3Smrg .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA, 3227ec681f3Smrg .pNext = NULL, 3237ec681f3Smrg .implicit_sync = true, 3247ec681f3Smrg }; 3257ec681f3Smrg const VkExportMemoryAllocateInfo memory_export_info = { 3267ec681f3Smrg .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO, 3277ec681f3Smrg .pNext = &memory_wsi_info, 3287ec681f3Smrg .handleTypes = wsi->sw ? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT : 3297ec681f3Smrg VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, 3307ec681f3Smrg }; 3317ec681f3Smrg const VkMemoryDedicatedAllocateInfo memory_dedicated_info = { 3327ec681f3Smrg .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, 3337ec681f3Smrg .pNext = &memory_export_info, 3347ec681f3Smrg .image = image->image, 3357ec681f3Smrg .buffer = VK_NULL_HANDLE, 3367ec681f3Smrg }; 3377ec681f3Smrg const VkImportMemoryHostPointerInfoEXT host_ptr_info = { 3387ec681f3Smrg .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT, 3397ec681f3Smrg .pNext = &memory_dedicated_info, 3407ec681f3Smrg .pHostPointer = sw_host_ptr, 3417ec681f3Smrg .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT, 3427ec681f3Smrg }; 3437ec681f3Smrg const VkMemoryAllocateInfo memory_info = { 3447ec681f3Smrg .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, 3457ec681f3Smrg .pNext = sw_host_ptr ? (void *)&host_ptr_info : (void *)&memory_dedicated_info, 3467ec681f3Smrg .allocationSize = reqs.size, 3477ec681f3Smrg .memoryTypeIndex = select_memory_type(wsi, true, reqs.memoryTypeBits), 3487ec681f3Smrg }; 3497ec681f3Smrg result = wsi->AllocateMemory(chain->device, &memory_info, 3507ec681f3Smrg &chain->alloc, &image->memory); 3517ec681f3Smrg if (result != VK_SUCCESS) 3527ec681f3Smrg goto fail; 3537ec681f3Smrg 3547ec681f3Smrg result = wsi->BindImageMemory(chain->device, image->image, 3557ec681f3Smrg image->memory, 0); 3567ec681f3Smrg if (result != VK_SUCCESS) 3577ec681f3Smrg goto fail; 3587ec681f3Smrg 3597ec681f3Smrg int fd = -1; 3607ec681f3Smrg if (!wsi->sw) { 3617ec681f3Smrg const VkMemoryGetFdInfoKHR memory_get_fd_info = { 3627ec681f3Smrg .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR, 3637ec681f3Smrg .pNext = NULL, 3647ec681f3Smrg .memory = image->memory, 3657ec681f3Smrg .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, 3667ec681f3Smrg }; 3677ec681f3Smrg 3687ec681f3Smrg result = wsi->GetMemoryFdKHR(chain->device, &memory_get_fd_info, &fd); 3697ec681f3Smrg if (result != VK_SUCCESS) 3707ec681f3Smrg goto fail; 3717ec681f3Smrg } 3727ec681f3Smrg 3737ec681f3Smrg if (!wsi->sw && num_modifier_lists > 0) { 3747ec681f3Smrg VkImageDrmFormatModifierPropertiesEXT image_mod_props = { 3757ec681f3Smrg .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT, 3767ec681f3Smrg }; 3777ec681f3Smrg result = wsi->GetImageDrmFormatModifierPropertiesEXT(chain->device, 3787ec681f3Smrg image->image, 3797ec681f3Smrg &image_mod_props); 3807ec681f3Smrg if (result != VK_SUCCESS) { 3817ec681f3Smrg close(fd); 3827ec681f3Smrg goto fail; 3837ec681f3Smrg } 3847ec681f3Smrg image->drm_modifier = image_mod_props.drmFormatModifier; 3857ec681f3Smrg assert(image->drm_modifier != DRM_FORMAT_MOD_INVALID); 3867ec681f3Smrg 3877ec681f3Smrg for (uint32_t j = 0; j < modifier_prop_count; j++) { 3887ec681f3Smrg if (modifier_props[j].drmFormatModifier == image->drm_modifier) { 3897ec681f3Smrg image->num_planes = modifier_props[j].drmFormatModifierPlaneCount; 3907ec681f3Smrg break; 3917ec681f3Smrg } 3927ec681f3Smrg } 3937ec681f3Smrg 3947ec681f3Smrg for (uint32_t p = 0; p < image->num_planes; p++) { 3957ec681f3Smrg const VkImageSubresource image_subresource = { 3967ec681f3Smrg .aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT << p, 3977ec681f3Smrg .mipLevel = 0, 3987ec681f3Smrg .arrayLayer = 0, 3997ec681f3Smrg }; 4007ec681f3Smrg VkSubresourceLayout image_layout; 4017ec681f3Smrg wsi->GetImageSubresourceLayout(chain->device, image->image, 4027ec681f3Smrg &image_subresource, &image_layout); 4037ec681f3Smrg image->sizes[p] = image_layout.size; 4047ec681f3Smrg image->row_pitches[p] = image_layout.rowPitch; 4057ec681f3Smrg image->offsets[p] = image_layout.offset; 4067ec681f3Smrg if (p == 0) { 4077ec681f3Smrg image->fds[p] = fd; 4087ec681f3Smrg } else { 4097ec681f3Smrg image->fds[p] = os_dupfd_cloexec(fd); 4107ec681f3Smrg if (image->fds[p] == -1) { 4117ec681f3Smrg for (uint32_t i = 0; i < p; i++) 4127ec681f3Smrg close(image->fds[i]); 4137ec681f3Smrg 4147ec681f3Smrg result = VK_ERROR_OUT_OF_HOST_MEMORY; 4157ec681f3Smrg goto fail; 4167ec681f3Smrg } 4177ec681f3Smrg } 4187ec681f3Smrg } 4197ec681f3Smrg } else { 4207ec681f3Smrg const VkImageSubresource image_subresource = { 4217ec681f3Smrg .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, 4227ec681f3Smrg .mipLevel = 0, 4237ec681f3Smrg .arrayLayer = 0, 4247ec681f3Smrg }; 4257ec681f3Smrg VkSubresourceLayout image_layout; 4267ec681f3Smrg wsi->GetImageSubresourceLayout(chain->device, image->image, 4277ec681f3Smrg &image_subresource, &image_layout); 4287ec681f3Smrg 4297ec681f3Smrg image->drm_modifier = DRM_FORMAT_MOD_INVALID; 4307ec681f3Smrg image->num_planes = 1; 4317ec681f3Smrg image->sizes[0] = reqs.size; 4327ec681f3Smrg image->row_pitches[0] = image_layout.rowPitch; 4337ec681f3Smrg image->offsets[0] = 0; 4347ec681f3Smrg image->fds[0] = fd; 4357ec681f3Smrg } 4367ec681f3Smrg 4377ec681f3Smrg vk_free(&chain->alloc, modifier_props); 4387ec681f3Smrg vk_free(&chain->alloc, image_modifiers); 4397ec681f3Smrg 4407ec681f3Smrg return VK_SUCCESS; 4417ec681f3Smrg 4427ec681f3Smrgfail: 4437ec681f3Smrg vk_free(&chain->alloc, modifier_props); 4447ec681f3Smrg vk_free(&chain->alloc, image_modifiers); 4457ec681f3Smrg wsi_destroy_image(chain, image); 4467ec681f3Smrg 4477ec681f3Smrg return result; 4487ec681f3Smrg} 4497ec681f3Smrg 4507ec681f3Smrgstatic inline uint32_t 4517ec681f3Smrgalign_u32(uint32_t v, uint32_t a) 4527ec681f3Smrg{ 4537ec681f3Smrg assert(a != 0 && a == (a & -a)); 4547ec681f3Smrg return (v + a - 1) & ~(a - 1); 4557ec681f3Smrg} 4567ec681f3Smrg 4577ec681f3Smrg#define WSI_PRIME_LINEAR_STRIDE_ALIGN 256 4587ec681f3Smrg 4597ec681f3SmrgVkResult 4607ec681f3Smrgwsi_create_prime_image(const struct wsi_swapchain *chain, 4617ec681f3Smrg const VkSwapchainCreateInfoKHR *pCreateInfo, 4627ec681f3Smrg bool use_modifier, 4637ec681f3Smrg struct wsi_image *image) 4647ec681f3Smrg{ 4657ec681f3Smrg const struct wsi_device *wsi = chain->wsi; 4667ec681f3Smrg VkResult result; 4677ec681f3Smrg 4687ec681f3Smrg memset(image, 0, sizeof(*image)); 4697ec681f3Smrg 4707ec681f3Smrg const uint32_t cpp = vk_format_size(pCreateInfo->imageFormat); 4717ec681f3Smrg const uint32_t linear_stride = align_u32(pCreateInfo->imageExtent.width * cpp, 4727ec681f3Smrg WSI_PRIME_LINEAR_STRIDE_ALIGN); 4737ec681f3Smrg 4747ec681f3Smrg uint32_t linear_size = linear_stride * pCreateInfo->imageExtent.height; 4757ec681f3Smrg linear_size = align_u32(linear_size, 4096); 4767ec681f3Smrg 4777ec681f3Smrg const VkExternalMemoryBufferCreateInfo prime_buffer_external_info = { 4787ec681f3Smrg .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO, 4797ec681f3Smrg .pNext = NULL, 4807ec681f3Smrg .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, 4817ec681f3Smrg }; 4827ec681f3Smrg const VkBufferCreateInfo prime_buffer_info = { 4837ec681f3Smrg .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, 4847ec681f3Smrg .pNext = &prime_buffer_external_info, 4857ec681f3Smrg .size = linear_size, 4867ec681f3Smrg .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT, 4877ec681f3Smrg .sharingMode = VK_SHARING_MODE_EXCLUSIVE, 4887ec681f3Smrg }; 4897ec681f3Smrg result = wsi->CreateBuffer(chain->device, &prime_buffer_info, 4907ec681f3Smrg &chain->alloc, &image->prime.buffer); 4917ec681f3Smrg if (result != VK_SUCCESS) 4927ec681f3Smrg goto fail; 4937ec681f3Smrg 4947ec681f3Smrg VkMemoryRequirements reqs; 4957ec681f3Smrg wsi->GetBufferMemoryRequirements(chain->device, image->prime.buffer, &reqs); 4967ec681f3Smrg assert(reqs.size <= linear_size); 4977ec681f3Smrg 4987ec681f3Smrg const struct wsi_memory_allocate_info memory_wsi_info = { 4997ec681f3Smrg .sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA, 5007ec681f3Smrg .pNext = NULL, 5017ec681f3Smrg .implicit_sync = true, 5027ec681f3Smrg }; 5037ec681f3Smrg const VkExportMemoryAllocateInfo prime_memory_export_info = { 5047ec681f3Smrg .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO, 5057ec681f3Smrg .pNext = &memory_wsi_info, 5067ec681f3Smrg .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, 5077ec681f3Smrg }; 5087ec681f3Smrg const VkMemoryDedicatedAllocateInfo prime_memory_dedicated_info = { 5097ec681f3Smrg .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, 5107ec681f3Smrg .pNext = &prime_memory_export_info, 5117ec681f3Smrg .image = VK_NULL_HANDLE, 5127ec681f3Smrg .buffer = image->prime.buffer, 5137ec681f3Smrg }; 5147ec681f3Smrg const VkMemoryAllocateInfo prime_memory_info = { 5157ec681f3Smrg .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, 5167ec681f3Smrg .pNext = &prime_memory_dedicated_info, 5177ec681f3Smrg .allocationSize = linear_size, 5187ec681f3Smrg .memoryTypeIndex = select_memory_type(wsi, false, reqs.memoryTypeBits), 5197ec681f3Smrg }; 5207ec681f3Smrg result = wsi->AllocateMemory(chain->device, &prime_memory_info, 5217ec681f3Smrg &chain->alloc, &image->prime.memory); 5227ec681f3Smrg if (result != VK_SUCCESS) 5237ec681f3Smrg goto fail; 5247ec681f3Smrg 5257ec681f3Smrg result = wsi->BindBufferMemory(chain->device, image->prime.buffer, 5267ec681f3Smrg image->prime.memory, 0); 5277ec681f3Smrg if (result != VK_SUCCESS) 5287ec681f3Smrg goto fail; 5297ec681f3Smrg 5307ec681f3Smrg const struct wsi_image_create_info image_wsi_info = { 5317ec681f3Smrg .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA, 5327ec681f3Smrg .prime_blit_src = true, 5337ec681f3Smrg }; 5347ec681f3Smrg VkImageCreateInfo image_info = { 5357ec681f3Smrg .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, 5367ec681f3Smrg .pNext = &image_wsi_info, 5377ec681f3Smrg .flags = 0, 5387ec681f3Smrg .imageType = VK_IMAGE_TYPE_2D, 5397ec681f3Smrg .format = pCreateInfo->imageFormat, 5407ec681f3Smrg .extent = { 5417ec681f3Smrg .width = pCreateInfo->imageExtent.width, 5427ec681f3Smrg .height = pCreateInfo->imageExtent.height, 5437ec681f3Smrg .depth = 1, 5447ec681f3Smrg }, 5457ec681f3Smrg .mipLevels = 1, 5467ec681f3Smrg .arrayLayers = 1, 5477ec681f3Smrg .samples = VK_SAMPLE_COUNT_1_BIT, 5487ec681f3Smrg .tiling = VK_IMAGE_TILING_OPTIMAL, 5497ec681f3Smrg .usage = pCreateInfo->imageUsage | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, 5507ec681f3Smrg .sharingMode = pCreateInfo->imageSharingMode, 5517ec681f3Smrg .queueFamilyIndexCount = pCreateInfo->queueFamilyIndexCount, 5527ec681f3Smrg .pQueueFamilyIndices = pCreateInfo->pQueueFamilyIndices, 5537ec681f3Smrg .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, 5547ec681f3Smrg }; 5557ec681f3Smrg if (pCreateInfo->flags & VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR) { 5567ec681f3Smrg image_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT | 5577ec681f3Smrg VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR; 5587ec681f3Smrg } 5597ec681f3Smrg result = wsi->CreateImage(chain->device, &image_info, 5607ec681f3Smrg &chain->alloc, &image->image); 5617ec681f3Smrg if (result != VK_SUCCESS) 5627ec681f3Smrg goto fail; 5637ec681f3Smrg 5647ec681f3Smrg wsi->GetImageMemoryRequirements(chain->device, image->image, &reqs); 5657ec681f3Smrg 5667ec681f3Smrg const VkMemoryDedicatedAllocateInfo memory_dedicated_info = { 5677ec681f3Smrg .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, 5687ec681f3Smrg .pNext = NULL, 5697ec681f3Smrg .image = image->image, 5707ec681f3Smrg .buffer = VK_NULL_HANDLE, 5717ec681f3Smrg }; 5727ec681f3Smrg const VkMemoryAllocateInfo memory_info = { 5737ec681f3Smrg .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, 5747ec681f3Smrg .pNext = &memory_dedicated_info, 5757ec681f3Smrg .allocationSize = reqs.size, 5767ec681f3Smrg .memoryTypeIndex = select_memory_type(wsi, true, reqs.memoryTypeBits), 5777ec681f3Smrg }; 5787ec681f3Smrg result = wsi->AllocateMemory(chain->device, &memory_info, 5797ec681f3Smrg &chain->alloc, &image->memory); 5807ec681f3Smrg if (result != VK_SUCCESS) 5817ec681f3Smrg goto fail; 5827ec681f3Smrg 5837ec681f3Smrg result = wsi->BindImageMemory(chain->device, image->image, 5847ec681f3Smrg image->memory, 0); 5857ec681f3Smrg if (result != VK_SUCCESS) 5867ec681f3Smrg goto fail; 5877ec681f3Smrg 5887ec681f3Smrg image->prime.blit_cmd_buffers = 5897ec681f3Smrg vk_zalloc(&chain->alloc, 5907ec681f3Smrg sizeof(VkCommandBuffer) * wsi->queue_family_count, 8, 5917ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); 5927ec681f3Smrg if (!image->prime.blit_cmd_buffers) { 5937ec681f3Smrg result = VK_ERROR_OUT_OF_HOST_MEMORY; 5947ec681f3Smrg goto fail; 5957ec681f3Smrg } 5967ec681f3Smrg 5977ec681f3Smrg for (uint32_t i = 0; i < wsi->queue_family_count; i++) { 5987ec681f3Smrg const VkCommandBufferAllocateInfo cmd_buffer_info = { 5997ec681f3Smrg .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, 6007ec681f3Smrg .pNext = NULL, 6017ec681f3Smrg .commandPool = chain->cmd_pools[i], 6027ec681f3Smrg .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, 6037ec681f3Smrg .commandBufferCount = 1, 6047ec681f3Smrg }; 6057ec681f3Smrg result = wsi->AllocateCommandBuffers(chain->device, &cmd_buffer_info, 6067ec681f3Smrg &image->prime.blit_cmd_buffers[i]); 6077ec681f3Smrg if (result != VK_SUCCESS) 6087ec681f3Smrg goto fail; 6097ec681f3Smrg 6107ec681f3Smrg const VkCommandBufferBeginInfo begin_info = { 6117ec681f3Smrg .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, 6127ec681f3Smrg }; 6137ec681f3Smrg wsi->BeginCommandBuffer(image->prime.blit_cmd_buffers[i], &begin_info); 6147ec681f3Smrg 6157ec681f3Smrg struct VkBufferImageCopy buffer_image_copy = { 6167ec681f3Smrg .bufferOffset = 0, 6177ec681f3Smrg .bufferRowLength = linear_stride / cpp, 6187ec681f3Smrg .bufferImageHeight = 0, 6197ec681f3Smrg .imageSubresource = { 6207ec681f3Smrg .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, 6217ec681f3Smrg .mipLevel = 0, 6227ec681f3Smrg .baseArrayLayer = 0, 6237ec681f3Smrg .layerCount = 1, 6247ec681f3Smrg }, 6257ec681f3Smrg .imageOffset = { .x = 0, .y = 0, .z = 0 }, 6267ec681f3Smrg .imageExtent = { 6277ec681f3Smrg .width = pCreateInfo->imageExtent.width, 6287ec681f3Smrg .height = pCreateInfo->imageExtent.height, 6297ec681f3Smrg .depth = 1, 6307ec681f3Smrg }, 6317ec681f3Smrg }; 6327ec681f3Smrg wsi->CmdCopyImageToBuffer(image->prime.blit_cmd_buffers[i], 6337ec681f3Smrg image->image, 6347ec681f3Smrg VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, 6357ec681f3Smrg image->prime.buffer, 6367ec681f3Smrg 1, &buffer_image_copy); 6377ec681f3Smrg 6387ec681f3Smrg result = wsi->EndCommandBuffer(image->prime.blit_cmd_buffers[i]); 6397ec681f3Smrg if (result != VK_SUCCESS) 6407ec681f3Smrg goto fail; 6417ec681f3Smrg } 6427ec681f3Smrg 6437ec681f3Smrg const VkMemoryGetFdInfoKHR linear_memory_get_fd_info = { 6447ec681f3Smrg .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR, 6457ec681f3Smrg .pNext = NULL, 6467ec681f3Smrg .memory = image->prime.memory, 6477ec681f3Smrg .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, 6487ec681f3Smrg }; 6497ec681f3Smrg int fd; 6507ec681f3Smrg result = wsi->GetMemoryFdKHR(chain->device, &linear_memory_get_fd_info, &fd); 6517ec681f3Smrg if (result != VK_SUCCESS) 6527ec681f3Smrg goto fail; 6537ec681f3Smrg 6547ec681f3Smrg image->drm_modifier = use_modifier ? DRM_FORMAT_MOD_LINEAR : DRM_FORMAT_MOD_INVALID; 6557ec681f3Smrg image->num_planes = 1; 6567ec681f3Smrg image->sizes[0] = linear_size; 6577ec681f3Smrg image->row_pitches[0] = linear_stride; 6587ec681f3Smrg image->offsets[0] = 0; 6597ec681f3Smrg image->fds[0] = fd; 6607ec681f3Smrg 6617ec681f3Smrg return VK_SUCCESS; 6627ec681f3Smrg 6637ec681f3Smrgfail: 6647ec681f3Smrg wsi_destroy_image(chain, image); 6657ec681f3Smrg 6667ec681f3Smrg return result; 6677ec681f3Smrg} 6687ec681f3Smrg 669