17ec681f3Smrg/* 27ec681f3Smrg * Copyright © 2020 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 "vk_object.h" 257ec681f3Smrg 267ec681f3Smrg#include "vk_alloc.h" 277ec681f3Smrg#include "vk_common_entrypoints.h" 287ec681f3Smrg#include "vk_device.h" 297ec681f3Smrg#include "util/hash_table.h" 307ec681f3Smrg#include "util/ralloc.h" 317ec681f3Smrg#include "vk_enum_to_str.h" 327ec681f3Smrg 337ec681f3Smrgvoid 347ec681f3Smrgvk_object_base_init(struct vk_device *device, 357ec681f3Smrg struct vk_object_base *base, 367ec681f3Smrg UNUSED VkObjectType obj_type) 377ec681f3Smrg{ 387ec681f3Smrg base->_loader_data.loaderMagic = ICD_LOADER_MAGIC; 397ec681f3Smrg base->type = obj_type; 407ec681f3Smrg base->device = device; 417ec681f3Smrg base->client_visible = false; 427ec681f3Smrg base->object_name = NULL; 437ec681f3Smrg util_sparse_array_init(&base->private_data, sizeof(uint64_t), 8); 447ec681f3Smrg} 457ec681f3Smrg 467ec681f3Smrgvoid 477ec681f3Smrgvk_object_base_finish(struct vk_object_base *base) 487ec681f3Smrg{ 497ec681f3Smrg util_sparse_array_finish(&base->private_data); 507ec681f3Smrg 517ec681f3Smrg if (base->object_name != NULL) 527ec681f3Smrg vk_free(&base->device->alloc, base->object_name); 537ec681f3Smrg} 547ec681f3Smrg 557ec681f3Smrgvoid * 567ec681f3Smrgvk_object_alloc(struct vk_device *device, 577ec681f3Smrg const VkAllocationCallbacks *alloc, 587ec681f3Smrg size_t size, 597ec681f3Smrg VkObjectType obj_type) 607ec681f3Smrg{ 617ec681f3Smrg void *ptr = vk_alloc2(&device->alloc, alloc, size, 8, 627ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); 637ec681f3Smrg if (ptr == NULL) 647ec681f3Smrg return NULL; 657ec681f3Smrg 667ec681f3Smrg vk_object_base_init(device, (struct vk_object_base *)ptr, obj_type); 677ec681f3Smrg 687ec681f3Smrg return ptr; 697ec681f3Smrg} 707ec681f3Smrg 717ec681f3Smrgvoid * 727ec681f3Smrgvk_object_zalloc(struct vk_device *device, 737ec681f3Smrg const VkAllocationCallbacks *alloc, 747ec681f3Smrg size_t size, 757ec681f3Smrg VkObjectType obj_type) 767ec681f3Smrg{ 777ec681f3Smrg void *ptr = vk_zalloc2(&device->alloc, alloc, size, 8, 787ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); 797ec681f3Smrg if (ptr == NULL) 807ec681f3Smrg return NULL; 817ec681f3Smrg 827ec681f3Smrg vk_object_base_init(device, (struct vk_object_base *)ptr, obj_type); 837ec681f3Smrg 847ec681f3Smrg return ptr; 857ec681f3Smrg} 867ec681f3Smrg 877ec681f3Smrgvoid * 887ec681f3Smrgvk_object_multialloc(struct vk_device *device, 897ec681f3Smrg struct vk_multialloc *ma, 907ec681f3Smrg const VkAllocationCallbacks *alloc, 917ec681f3Smrg VkObjectType obj_type) 927ec681f3Smrg{ 937ec681f3Smrg void *ptr = vk_multialloc_alloc2(ma, &device->alloc, alloc, 947ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); 957ec681f3Smrg if (ptr == NULL) 967ec681f3Smrg return NULL; 977ec681f3Smrg 987ec681f3Smrg vk_object_base_init(device, (struct vk_object_base *)ptr, obj_type); 997ec681f3Smrg 1007ec681f3Smrg return ptr; 1017ec681f3Smrg} 1027ec681f3Smrg 1037ec681f3Smrgvoid * 1047ec681f3Smrgvk_object_multizalloc(struct vk_device *device, 1057ec681f3Smrg struct vk_multialloc *ma, 1067ec681f3Smrg const VkAllocationCallbacks *alloc, 1077ec681f3Smrg VkObjectType obj_type) 1087ec681f3Smrg{ 1097ec681f3Smrg void *ptr = vk_multialloc_zalloc2(ma, &device->alloc, alloc, 1107ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); 1117ec681f3Smrg if (ptr == NULL) 1127ec681f3Smrg return NULL; 1137ec681f3Smrg 1147ec681f3Smrg vk_object_base_init(device, (struct vk_object_base *)ptr, obj_type); 1157ec681f3Smrg 1167ec681f3Smrg return ptr; 1177ec681f3Smrg} 1187ec681f3Smrg 1197ec681f3Smrgvoid 1207ec681f3Smrgvk_object_free(struct vk_device *device, 1217ec681f3Smrg const VkAllocationCallbacks *alloc, 1227ec681f3Smrg void *data) 1237ec681f3Smrg{ 1247ec681f3Smrg vk_object_base_finish((struct vk_object_base *)data); 1257ec681f3Smrg vk_free2(&device->alloc, alloc, data); 1267ec681f3Smrg} 1277ec681f3Smrg 1287ec681f3SmrgVkResult 1297ec681f3Smrgvk_private_data_slot_create(struct vk_device *device, 1307ec681f3Smrg const VkPrivateDataSlotCreateInfoEXT* pCreateInfo, 1317ec681f3Smrg const VkAllocationCallbacks* pAllocator, 1327ec681f3Smrg VkPrivateDataSlotEXT* pPrivateDataSlot) 1337ec681f3Smrg{ 1347ec681f3Smrg struct vk_private_data_slot *slot = 1357ec681f3Smrg vk_alloc2(&device->alloc, pAllocator, sizeof(*slot), 8, 1367ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); 1377ec681f3Smrg if (slot == NULL) 1387ec681f3Smrg return VK_ERROR_OUT_OF_HOST_MEMORY; 1397ec681f3Smrg 1407ec681f3Smrg vk_object_base_init(device, &slot->base, 1417ec681f3Smrg VK_OBJECT_TYPE_PRIVATE_DATA_SLOT_EXT); 1427ec681f3Smrg slot->index = p_atomic_inc_return(&device->private_data_next_index); 1437ec681f3Smrg 1447ec681f3Smrg *pPrivateDataSlot = vk_private_data_slot_to_handle(slot); 1457ec681f3Smrg 1467ec681f3Smrg return VK_SUCCESS; 1477ec681f3Smrg} 1487ec681f3Smrg 1497ec681f3Smrgvoid 1507ec681f3Smrgvk_private_data_slot_destroy(struct vk_device *device, 1517ec681f3Smrg VkPrivateDataSlotEXT privateDataSlot, 1527ec681f3Smrg const VkAllocationCallbacks *pAllocator) 1537ec681f3Smrg{ 1547ec681f3Smrg VK_FROM_HANDLE(vk_private_data_slot, slot, privateDataSlot); 1557ec681f3Smrg if (slot == NULL) 1567ec681f3Smrg return; 1577ec681f3Smrg 1587ec681f3Smrg vk_object_base_finish(&slot->base); 1597ec681f3Smrg vk_free2(&device->alloc, pAllocator, slot); 1607ec681f3Smrg} 1617ec681f3Smrg 1627ec681f3Smrg#ifdef ANDROID 1637ec681f3Smrgstatic VkResult 1647ec681f3Smrgget_swapchain_private_data_locked(struct vk_device *device, 1657ec681f3Smrg uint64_t objectHandle, 1667ec681f3Smrg struct vk_private_data_slot *slot, 1677ec681f3Smrg uint64_t **private_data) 1687ec681f3Smrg{ 1697ec681f3Smrg if (unlikely(device->swapchain_private == NULL)) { 1707ec681f3Smrg /* Even though VkSwapchain is a non-dispatchable object, we know a 1717ec681f3Smrg * priori that Android swapchains are actually pointers so we can use 1727ec681f3Smrg * the pointer hash table for them. 1737ec681f3Smrg */ 1747ec681f3Smrg device->swapchain_private = _mesa_pointer_hash_table_create(NULL); 1757ec681f3Smrg if (device->swapchain_private == NULL) 1767ec681f3Smrg return VK_ERROR_OUT_OF_HOST_MEMORY; 1777ec681f3Smrg } 1787ec681f3Smrg 1797ec681f3Smrg struct hash_entry *entry = 1807ec681f3Smrg _mesa_hash_table_search(device->swapchain_private, 1817ec681f3Smrg (void *)(uintptr_t)objectHandle); 1827ec681f3Smrg if (unlikely(entry == NULL)) { 1837ec681f3Smrg struct util_sparse_array *swapchain_private = 1847ec681f3Smrg ralloc(device->swapchain_private, struct util_sparse_array); 1857ec681f3Smrg util_sparse_array_init(swapchain_private, sizeof(uint64_t), 8); 1867ec681f3Smrg 1877ec681f3Smrg entry = _mesa_hash_table_insert(device->swapchain_private, 1887ec681f3Smrg (void *)(uintptr_t)objectHandle, 1897ec681f3Smrg swapchain_private); 1907ec681f3Smrg if (entry == NULL) 1917ec681f3Smrg return VK_ERROR_OUT_OF_HOST_MEMORY; 1927ec681f3Smrg } 1937ec681f3Smrg 1947ec681f3Smrg struct util_sparse_array *swapchain_private = entry->data; 1957ec681f3Smrg *private_data = util_sparse_array_get(swapchain_private, slot->index); 1967ec681f3Smrg 1977ec681f3Smrg return VK_SUCCESS; 1987ec681f3Smrg} 1997ec681f3Smrg#endif /* ANDROID */ 2007ec681f3Smrg 2017ec681f3Smrgstatic VkResult 2027ec681f3Smrgvk_object_base_private_data(struct vk_device *device, 2037ec681f3Smrg VkObjectType objectType, 2047ec681f3Smrg uint64_t objectHandle, 2057ec681f3Smrg VkPrivateDataSlotEXT privateDataSlot, 2067ec681f3Smrg uint64_t **private_data) 2077ec681f3Smrg{ 2087ec681f3Smrg VK_FROM_HANDLE(vk_private_data_slot, slot, privateDataSlot); 2097ec681f3Smrg 2107ec681f3Smrg#ifdef ANDROID 2117ec681f3Smrg /* There is an annoying spec corner here on Android. Because WSI is 2127ec681f3Smrg * implemented in the Vulkan loader which doesn't know about the 2137ec681f3Smrg * VK_EXT_private_data extension, we have to handle VkSwapchainKHR in the 2147ec681f3Smrg * driver as a special case. On future versions of Android where the 2157ec681f3Smrg * loader does understand VK_EXT_private_data, we'll never see a 2167ec681f3Smrg * vkGet/SetPrivateDataEXT call on a swapchain because the loader will 2177ec681f3Smrg * handle it. 2187ec681f3Smrg */ 2197ec681f3Smrg if (objectType == VK_OBJECT_TYPE_SWAPCHAIN_KHR) { 2207ec681f3Smrg mtx_lock(&device->swapchain_private_mtx); 2217ec681f3Smrg VkResult result = get_swapchain_private_data_locked(device, objectHandle, 2227ec681f3Smrg slot, private_data); 2237ec681f3Smrg mtx_unlock(&device->swapchain_private_mtx); 2247ec681f3Smrg return result; 2257ec681f3Smrg } 2267ec681f3Smrg#endif /* ANDROID */ 2277ec681f3Smrg 2287ec681f3Smrg struct vk_object_base *obj = 2297ec681f3Smrg vk_object_base_from_u64_handle(objectHandle, objectType); 2307ec681f3Smrg *private_data = util_sparse_array_get(&obj->private_data, slot->index); 2317ec681f3Smrg 2327ec681f3Smrg return VK_SUCCESS; 2337ec681f3Smrg} 2347ec681f3Smrg 2357ec681f3SmrgVkResult 2367ec681f3Smrgvk_object_base_set_private_data(struct vk_device *device, 2377ec681f3Smrg VkObjectType objectType, 2387ec681f3Smrg uint64_t objectHandle, 2397ec681f3Smrg VkPrivateDataSlotEXT privateDataSlot, 2407ec681f3Smrg uint64_t data) 2417ec681f3Smrg{ 2427ec681f3Smrg uint64_t *private_data; 2437ec681f3Smrg VkResult result = vk_object_base_private_data(device, 2447ec681f3Smrg objectType, objectHandle, 2457ec681f3Smrg privateDataSlot, 2467ec681f3Smrg &private_data); 2477ec681f3Smrg if (unlikely(result != VK_SUCCESS)) 2487ec681f3Smrg return result; 2497ec681f3Smrg 2507ec681f3Smrg *private_data = data; 2517ec681f3Smrg return VK_SUCCESS; 2527ec681f3Smrg} 2537ec681f3Smrg 2547ec681f3Smrgvoid 2557ec681f3Smrgvk_object_base_get_private_data(struct vk_device *device, 2567ec681f3Smrg VkObjectType objectType, 2577ec681f3Smrg uint64_t objectHandle, 2587ec681f3Smrg VkPrivateDataSlotEXT privateDataSlot, 2597ec681f3Smrg uint64_t *pData) 2607ec681f3Smrg{ 2617ec681f3Smrg uint64_t *private_data; 2627ec681f3Smrg VkResult result = vk_object_base_private_data(device, 2637ec681f3Smrg objectType, objectHandle, 2647ec681f3Smrg privateDataSlot, 2657ec681f3Smrg &private_data); 2667ec681f3Smrg if (likely(result == VK_SUCCESS)) { 2677ec681f3Smrg *pData = *private_data; 2687ec681f3Smrg } else { 2697ec681f3Smrg *pData = 0; 2707ec681f3Smrg } 2717ec681f3Smrg} 2727ec681f3Smrg 2737ec681f3SmrgVKAPI_ATTR VkResult VKAPI_CALL 2747ec681f3Smrgvk_common_CreatePrivateDataSlotEXT(VkDevice _device, 2757ec681f3Smrg const VkPrivateDataSlotCreateInfoEXT *pCreateInfo, 2767ec681f3Smrg const VkAllocationCallbacks *pAllocator, 2777ec681f3Smrg VkPrivateDataSlotEXT *pPrivateDataSlot) 2787ec681f3Smrg{ 2797ec681f3Smrg VK_FROM_HANDLE(vk_device, device, _device); 2807ec681f3Smrg return vk_private_data_slot_create(device, pCreateInfo, pAllocator, 2817ec681f3Smrg pPrivateDataSlot); 2827ec681f3Smrg} 2837ec681f3Smrg 2847ec681f3SmrgVKAPI_ATTR void VKAPI_CALL 2857ec681f3Smrgvk_common_DestroyPrivateDataSlotEXT(VkDevice _device, 2867ec681f3Smrg VkPrivateDataSlotEXT privateDataSlot, 2877ec681f3Smrg const VkAllocationCallbacks *pAllocator) 2887ec681f3Smrg{ 2897ec681f3Smrg VK_FROM_HANDLE(vk_device, device, _device); 2907ec681f3Smrg vk_private_data_slot_destroy(device, privateDataSlot, pAllocator); 2917ec681f3Smrg} 2927ec681f3Smrg 2937ec681f3SmrgVKAPI_ATTR VkResult VKAPI_CALL 2947ec681f3Smrgvk_common_SetPrivateDataEXT(VkDevice _device, 2957ec681f3Smrg VkObjectType objectType, 2967ec681f3Smrg uint64_t objectHandle, 2977ec681f3Smrg VkPrivateDataSlotEXT privateDataSlot, 2987ec681f3Smrg uint64_t data) 2997ec681f3Smrg{ 3007ec681f3Smrg VK_FROM_HANDLE(vk_device, device, _device); 3017ec681f3Smrg return vk_object_base_set_private_data(device, 3027ec681f3Smrg objectType, objectHandle, 3037ec681f3Smrg privateDataSlot, data); 3047ec681f3Smrg} 3057ec681f3Smrg 3067ec681f3SmrgVKAPI_ATTR void VKAPI_CALL 3077ec681f3Smrgvk_common_GetPrivateDataEXT(VkDevice _device, 3087ec681f3Smrg VkObjectType objectType, 3097ec681f3Smrg uint64_t objectHandle, 3107ec681f3Smrg VkPrivateDataSlotEXT privateDataSlot, 3117ec681f3Smrg uint64_t *pData) 3127ec681f3Smrg{ 3137ec681f3Smrg VK_FROM_HANDLE(vk_device, device, _device); 3147ec681f3Smrg vk_object_base_get_private_data(device, 3157ec681f3Smrg objectType, objectHandle, 3167ec681f3Smrg privateDataSlot, pData); 3177ec681f3Smrg} 3187ec681f3Smrg 3197ec681f3Smrgconst char * 3207ec681f3Smrgvk_object_base_name(struct vk_object_base *obj) 3217ec681f3Smrg{ 3227ec681f3Smrg if (obj->object_name) 3237ec681f3Smrg return obj->object_name; 3247ec681f3Smrg 3257ec681f3Smrg obj->object_name = vk_asprintf(&obj->device->alloc, 3267ec681f3Smrg VK_SYSTEM_ALLOCATION_SCOPE_DEVICE, 3277ec681f3Smrg "%s(0x%"PRIx64")", 3287ec681f3Smrg vk_ObjectType_to_ObjectName(obj->type), 3297ec681f3Smrg (uint64_t)(uintptr_t)obj); 3307ec681f3Smrg 3317ec681f3Smrg return obj->object_name; 3327ec681f3Smrg} 333