1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2017, Google Inc. 3bf215546Sopenharmony_ci * SPDX-License-Identifier: MIT 4bf215546Sopenharmony_ci */ 5bf215546Sopenharmony_ci 6bf215546Sopenharmony_ci#include "tu_android.h" 7bf215546Sopenharmony_ci 8bf215546Sopenharmony_ci#include <hardware/gralloc.h> 9bf215546Sopenharmony_ci 10bf215546Sopenharmony_ci#if ANDROID_API_LEVEL >= 26 11bf215546Sopenharmony_ci#include <hardware/gralloc1.h> 12bf215546Sopenharmony_ci#endif 13bf215546Sopenharmony_ci 14bf215546Sopenharmony_ci#include <hardware/hardware.h> 15bf215546Sopenharmony_ci#include <hardware/hwvulkan.h> 16bf215546Sopenharmony_ci 17bf215546Sopenharmony_ci#include "drm-uapi/drm_fourcc.h" 18bf215546Sopenharmony_ci 19bf215546Sopenharmony_ci#include "util/libsync.h" 20bf215546Sopenharmony_ci#include "util/os_file.h" 21bf215546Sopenharmony_ci 22bf215546Sopenharmony_ci#include "tu_device.h" 23bf215546Sopenharmony_ci#include "tu_image.h" 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_cistatic int 26bf215546Sopenharmony_citu_hal_open(const struct hw_module_t *mod, 27bf215546Sopenharmony_ci const char *id, 28bf215546Sopenharmony_ci struct hw_device_t **dev); 29bf215546Sopenharmony_cistatic int 30bf215546Sopenharmony_citu_hal_close(struct hw_device_t *dev); 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_cistatic void UNUSED 33bf215546Sopenharmony_cistatic_asserts(void) 34bf215546Sopenharmony_ci{ 35bf215546Sopenharmony_ci STATIC_ASSERT(HWVULKAN_DISPATCH_MAGIC == ICD_LOADER_MAGIC); 36bf215546Sopenharmony_ci} 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ciPUBLIC struct hwvulkan_module_t HAL_MODULE_INFO_SYM = { 39bf215546Sopenharmony_ci .common = 40bf215546Sopenharmony_ci { 41bf215546Sopenharmony_ci .tag = HARDWARE_MODULE_TAG, 42bf215546Sopenharmony_ci .module_api_version = HWVULKAN_MODULE_API_VERSION_0_1, 43bf215546Sopenharmony_ci .hal_api_version = HARDWARE_MAKE_API_VERSION(1, 0), 44bf215546Sopenharmony_ci .id = HWVULKAN_HARDWARE_MODULE_ID, 45bf215546Sopenharmony_ci .name = "Turnip Vulkan HAL", 46bf215546Sopenharmony_ci .author = "Google", 47bf215546Sopenharmony_ci .methods = 48bf215546Sopenharmony_ci &(hw_module_methods_t){ 49bf215546Sopenharmony_ci .open = tu_hal_open, 50bf215546Sopenharmony_ci }, 51bf215546Sopenharmony_ci }, 52bf215546Sopenharmony_ci}; 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci/* If any bits in test_mask are set, then unset them and return true. */ 55bf215546Sopenharmony_cistatic inline bool 56bf215546Sopenharmony_ciunmask32(uint32_t *inout_mask, uint32_t test_mask) 57bf215546Sopenharmony_ci{ 58bf215546Sopenharmony_ci uint32_t orig_mask = *inout_mask; 59bf215546Sopenharmony_ci *inout_mask &= ~test_mask; 60bf215546Sopenharmony_ci return *inout_mask != orig_mask; 61bf215546Sopenharmony_ci} 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_cistatic int 64bf215546Sopenharmony_citu_hal_open(const struct hw_module_t *mod, 65bf215546Sopenharmony_ci const char *id, 66bf215546Sopenharmony_ci struct hw_device_t **dev) 67bf215546Sopenharmony_ci{ 68bf215546Sopenharmony_ci assert(mod == &HAL_MODULE_INFO_SYM.common); 69bf215546Sopenharmony_ci assert(strcmp(id, HWVULKAN_DEVICE_0) == 0); 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci hwvulkan_device_t *hal_dev = malloc(sizeof(*hal_dev)); 72bf215546Sopenharmony_ci if (!hal_dev) 73bf215546Sopenharmony_ci return -1; 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci *hal_dev = (hwvulkan_device_t){ 76bf215546Sopenharmony_ci .common = 77bf215546Sopenharmony_ci { 78bf215546Sopenharmony_ci .tag = HARDWARE_DEVICE_TAG, 79bf215546Sopenharmony_ci .version = HWVULKAN_DEVICE_API_VERSION_0_1, 80bf215546Sopenharmony_ci .module = &HAL_MODULE_INFO_SYM.common, 81bf215546Sopenharmony_ci .close = tu_hal_close, 82bf215546Sopenharmony_ci }, 83bf215546Sopenharmony_ci .EnumerateInstanceExtensionProperties = 84bf215546Sopenharmony_ci tu_EnumerateInstanceExtensionProperties, 85bf215546Sopenharmony_ci .CreateInstance = tu_CreateInstance, 86bf215546Sopenharmony_ci .GetInstanceProcAddr = tu_GetInstanceProcAddr, 87bf215546Sopenharmony_ci }; 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci *dev = &hal_dev->common; 90bf215546Sopenharmony_ci return 0; 91bf215546Sopenharmony_ci} 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_cistatic int 94bf215546Sopenharmony_citu_hal_close(struct hw_device_t *dev) 95bf215546Sopenharmony_ci{ 96bf215546Sopenharmony_ci /* hwvulkan.h claims that hw_device_t::close() is never called. */ 97bf215546Sopenharmony_ci return -1; 98bf215546Sopenharmony_ci} 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci/* get dma-buf and modifier from gralloc info */ 101bf215546Sopenharmony_cistatic VkResult 102bf215546Sopenharmony_citu_gralloc_info_other(struct tu_device *device, 103bf215546Sopenharmony_ci const VkNativeBufferANDROID *gralloc_info, 104bf215546Sopenharmony_ci int *dma_buf, 105bf215546Sopenharmony_ci uint64_t *modifier) 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci{ 108bf215546Sopenharmony_ci const uint32_t *handle_fds = (uint32_t *)gralloc_info->handle->data; 109bf215546Sopenharmony_ci const uint32_t *handle_data = &handle_fds[gralloc_info->handle->numFds]; 110bf215546Sopenharmony_ci bool ubwc = false; 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci if (gralloc_info->handle->numFds == 1) { 113bf215546Sopenharmony_ci /* gbm_gralloc. TODO: modifiers support */ 114bf215546Sopenharmony_ci *dma_buf = handle_fds[0]; 115bf215546Sopenharmony_ci } else if (gralloc_info->handle->numFds == 2) { 116bf215546Sopenharmony_ci /* Qualcomm gralloc, find it at: 117bf215546Sopenharmony_ci * 118bf215546Sopenharmony_ci * https://android.googlesource.com/platform/hardware/qcom/display/. 119bf215546Sopenharmony_ci * 120bf215546Sopenharmony_ci * The gralloc_info->handle is a pointer to a struct private_handle_t 121bf215546Sopenharmony_ci * from your platform's gralloc. On msm8996 (a5xx) and newer grallocs 122bf215546Sopenharmony_ci * that's libgralloc1/gr_priv_handle.h, while previously it was 123bf215546Sopenharmony_ci * libgralloc/gralloc_priv.h. 124bf215546Sopenharmony_ci */ 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci if (gralloc_info->handle->numInts < 2) { 127bf215546Sopenharmony_ci return vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE, 128bf215546Sopenharmony_ci "VkNativeBufferANDROID::handle::numInts is %d, " 129bf215546Sopenharmony_ci "expected at least 2 for qcom gralloc", 130bf215546Sopenharmony_ci gralloc_info->handle->numFds); 131bf215546Sopenharmony_ci } 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci uint32_t gmsm = ('g' << 24) | ('m' << 16) | ('s' << 8) | 'm'; 134bf215546Sopenharmony_ci if (handle_data[0] != gmsm) { 135bf215546Sopenharmony_ci return vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE, 136bf215546Sopenharmony_ci "private_handle_t::magic is %x, expected %x", 137bf215546Sopenharmony_ci handle_data[0], gmsm); 138bf215546Sopenharmony_ci } 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_ci /* This UBWC flag was introduced in a5xx. */ 141bf215546Sopenharmony_ci ubwc = handle_data[1] & 0x08000000; 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci /* QCOM gralloc has two fds passed in: the actual GPU buffer, and a buffer 144bf215546Sopenharmony_ci * of CPU-side metadata. I haven't found any need for the metadata buffer 145bf215546Sopenharmony_ci * yet. See qdMetaData.h for what's in the metadata fd. 146bf215546Sopenharmony_ci */ 147bf215546Sopenharmony_ci *dma_buf = handle_fds[0]; 148bf215546Sopenharmony_ci } else { 149bf215546Sopenharmony_ci return vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE, 150bf215546Sopenharmony_ci "VkNativeBufferANDROID::handle::numFds is %d, " 151bf215546Sopenharmony_ci "expected 1 (gbm_gralloc) or 2 (qcom gralloc)", 152bf215546Sopenharmony_ci gralloc_info->handle->numFds); 153bf215546Sopenharmony_ci } 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci *modifier = ubwc ? DRM_FORMAT_MOD_QCOM_COMPRESSED : DRM_FORMAT_MOD_LINEAR; 156bf215546Sopenharmony_ci return VK_SUCCESS; 157bf215546Sopenharmony_ci} 158bf215546Sopenharmony_ci 159bf215546Sopenharmony_cistatic const char cros_gralloc_module_name[] = "CrOS Gralloc"; 160bf215546Sopenharmony_ci 161bf215546Sopenharmony_ci#define CROS_GRALLOC_DRM_GET_BUFFER_INFO 4 162bf215546Sopenharmony_ci#define CROS_GRALLOC_DRM_GET_USAGE 5 163bf215546Sopenharmony_ci#define CROS_GRALLOC_DRM_GET_USAGE_FRONT_RENDERING_BIT 0x1 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_cistruct cros_gralloc0_buffer_info { 166bf215546Sopenharmony_ci uint32_t drm_fourcc; 167bf215546Sopenharmony_ci int num_fds; 168bf215546Sopenharmony_ci int fds[4]; 169bf215546Sopenharmony_ci uint64_t modifier; 170bf215546Sopenharmony_ci int offset[4]; 171bf215546Sopenharmony_ci int stride[4]; 172bf215546Sopenharmony_ci}; 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_cistatic VkResult 175bf215546Sopenharmony_citu_gralloc_info_cros(struct tu_device *device, 176bf215546Sopenharmony_ci const VkNativeBufferANDROID *gralloc_info, 177bf215546Sopenharmony_ci int *dma_buf, 178bf215546Sopenharmony_ci uint64_t *modifier) 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ci{ 181bf215546Sopenharmony_ci const gralloc_module_t *gralloc = device->gralloc; 182bf215546Sopenharmony_ci struct cros_gralloc0_buffer_info info; 183bf215546Sopenharmony_ci int ret; 184bf215546Sopenharmony_ci 185bf215546Sopenharmony_ci ret = gralloc->perform(gralloc, CROS_GRALLOC_DRM_GET_BUFFER_INFO, 186bf215546Sopenharmony_ci gralloc_info->handle, &info); 187bf215546Sopenharmony_ci if (ret) 188bf215546Sopenharmony_ci return VK_ERROR_INVALID_EXTERNAL_HANDLE; 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_ci *dma_buf = info.fds[0]; 191bf215546Sopenharmony_ci *modifier = info.modifier; 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_ci return VK_SUCCESS; 194bf215546Sopenharmony_ci} 195bf215546Sopenharmony_ci 196bf215546Sopenharmony_ciVkResult 197bf215546Sopenharmony_citu_gralloc_info(struct tu_device *device, 198bf215546Sopenharmony_ci const VkNativeBufferANDROID *gralloc_info, 199bf215546Sopenharmony_ci int *dma_buf, 200bf215546Sopenharmony_ci uint64_t *modifier) 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_ci{ 203bf215546Sopenharmony_ci if (!device->gralloc) { 204bf215546Sopenharmony_ci /* get gralloc module for gralloc buffer info query */ 205bf215546Sopenharmony_ci int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, 206bf215546Sopenharmony_ci (const hw_module_t **)&device->gralloc); 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_ci if (ret) { 209bf215546Sopenharmony_ci /* This is *slightly* awkward, but if we are asked to import 210bf215546Sopenharmony_ci * a gralloc handle, and there is no gralloc, it is some sort 211bf215546Sopenharmony_ci * of invalid handle. 212bf215546Sopenharmony_ci */ 213bf215546Sopenharmony_ci return vk_startup_errorf(device->instance, 214bf215546Sopenharmony_ci VK_ERROR_INVALID_EXTERNAL_HANDLE, 215bf215546Sopenharmony_ci "Could not open gralloc\n"); 216bf215546Sopenharmony_ci } 217bf215546Sopenharmony_ci 218bf215546Sopenharmony_ci const gralloc_module_t *gralloc = device->gralloc; 219bf215546Sopenharmony_ci 220bf215546Sopenharmony_ci mesa_logi("opened gralloc module name: %s", gralloc->common.name); 221bf215546Sopenharmony_ci 222bf215546Sopenharmony_ci /* TODO not sure qcom gralloc module name, but we should check 223bf215546Sopenharmony_ci * for it here and move the special gmsm handling out of 224bf215546Sopenharmony_ci * tu_gralloc_info_other() 225bf215546Sopenharmony_ci */ 226bf215546Sopenharmony_ci if (!strcmp(gralloc->common.name, cros_gralloc_module_name) && gralloc->perform) { 227bf215546Sopenharmony_ci device->gralloc_type = TU_GRALLOC_CROS; 228bf215546Sopenharmony_ci } else { 229bf215546Sopenharmony_ci device->gralloc_type = TU_GRALLOC_OTHER; 230bf215546Sopenharmony_ci } 231bf215546Sopenharmony_ci } 232bf215546Sopenharmony_ci 233bf215546Sopenharmony_ci if (device->gralloc_type == TU_GRALLOC_CROS) { 234bf215546Sopenharmony_ci return tu_gralloc_info_cros(device, gralloc_info, dma_buf, modifier); 235bf215546Sopenharmony_ci } else { 236bf215546Sopenharmony_ci return tu_gralloc_info_other(device, gralloc_info, dma_buf, modifier); 237bf215546Sopenharmony_ci } 238bf215546Sopenharmony_ci} 239bf215546Sopenharmony_ci 240bf215546Sopenharmony_ci/** 241bf215546Sopenharmony_ci * Creates the VkImage using the gralloc handle in *gralloc_info. 242bf215546Sopenharmony_ci * 243bf215546Sopenharmony_ci * We support two different grallocs here, gbm_gralloc, and the qcom gralloc 244bf215546Sopenharmony_ci * used on Android phones. 245bf215546Sopenharmony_ci */ 246bf215546Sopenharmony_ciVkResult 247bf215546Sopenharmony_citu_import_memory_from_gralloc_handle(VkDevice device_h, 248bf215546Sopenharmony_ci int dma_buf, 249bf215546Sopenharmony_ci const VkAllocationCallbacks *alloc, 250bf215546Sopenharmony_ci VkImage image_h) 251bf215546Sopenharmony_ci 252bf215546Sopenharmony_ci{ 253bf215546Sopenharmony_ci struct tu_image *image = NULL; 254bf215546Sopenharmony_ci VkResult result; 255bf215546Sopenharmony_ci 256bf215546Sopenharmony_ci image = tu_image_from_handle(image_h); 257bf215546Sopenharmony_ci 258bf215546Sopenharmony_ci VkDeviceMemory memory_h; 259bf215546Sopenharmony_ci 260bf215546Sopenharmony_ci const VkMemoryDedicatedAllocateInfo ded_alloc = { 261bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, 262bf215546Sopenharmony_ci .pNext = NULL, 263bf215546Sopenharmony_ci .buffer = VK_NULL_HANDLE, 264bf215546Sopenharmony_ci .image = image_h 265bf215546Sopenharmony_ci }; 266bf215546Sopenharmony_ci 267bf215546Sopenharmony_ci const VkImportMemoryFdInfoKHR import_info = { 268bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR, 269bf215546Sopenharmony_ci .pNext = &ded_alloc, 270bf215546Sopenharmony_ci .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT, 271bf215546Sopenharmony_ci .fd = os_dupfd_cloexec(dma_buf), 272bf215546Sopenharmony_ci }; 273bf215546Sopenharmony_ci 274bf215546Sopenharmony_ci result = 275bf215546Sopenharmony_ci tu_AllocateMemory(device_h, 276bf215546Sopenharmony_ci &(VkMemoryAllocateInfo) { 277bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, 278bf215546Sopenharmony_ci .pNext = &import_info, 279bf215546Sopenharmony_ci .allocationSize = image->total_size, 280bf215546Sopenharmony_ci .memoryTypeIndex = 0, 281bf215546Sopenharmony_ci }, 282bf215546Sopenharmony_ci alloc, &memory_h); 283bf215546Sopenharmony_ci if (result != VK_SUCCESS) 284bf215546Sopenharmony_ci goto fail_create_image; 285bf215546Sopenharmony_ci 286bf215546Sopenharmony_ci VkBindImageMemoryInfo bind_info = { 287bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO, 288bf215546Sopenharmony_ci .image = image_h, 289bf215546Sopenharmony_ci .memory = memory_h, 290bf215546Sopenharmony_ci .memoryOffset = 0, 291bf215546Sopenharmony_ci }; 292bf215546Sopenharmony_ci tu_BindImageMemory2(device_h, 1, &bind_info); 293bf215546Sopenharmony_ci 294bf215546Sopenharmony_ci image->owned_memory = memory_h; 295bf215546Sopenharmony_ci 296bf215546Sopenharmony_ci return VK_SUCCESS; 297bf215546Sopenharmony_ci 298bf215546Sopenharmony_cifail_create_image: 299bf215546Sopenharmony_ci tu_DestroyImage(device_h, image_h, alloc); 300bf215546Sopenharmony_ci 301bf215546Sopenharmony_ci return result; 302bf215546Sopenharmony_ci} 303bf215546Sopenharmony_ci 304bf215546Sopenharmony_cistatic VkResult 305bf215546Sopenharmony_ciformat_supported_with_usage(VkDevice device_h, VkFormat format, 306bf215546Sopenharmony_ci VkImageUsageFlags imageUsage) 307bf215546Sopenharmony_ci{ 308bf215546Sopenharmony_ci TU_FROM_HANDLE(tu_device, device, device_h); 309bf215546Sopenharmony_ci struct tu_physical_device *phys_dev = device->physical_device; 310bf215546Sopenharmony_ci VkPhysicalDevice phys_dev_h = tu_physical_device_to_handle(phys_dev); 311bf215546Sopenharmony_ci VkResult result; 312bf215546Sopenharmony_ci 313bf215546Sopenharmony_ci /* WARNING: Android Nougat's libvulkan.so hardcodes the VkImageUsageFlags 314bf215546Sopenharmony_ci * returned to applications via 315bf215546Sopenharmony_ci * VkSurfaceCapabilitiesKHR::supportedUsageFlags. 316bf215546Sopenharmony_ci * The relevant code in libvulkan/swapchain.cpp contains this fun comment: 317bf215546Sopenharmony_ci * 318bf215546Sopenharmony_ci * TODO(jessehall): I think these are right, but haven't thought hard 319bf215546Sopenharmony_ci * about it. Do we need to query the driver for support of any of 320bf215546Sopenharmony_ci * these? 321bf215546Sopenharmony_ci * 322bf215546Sopenharmony_ci * Any disagreement between this function and the hardcoded 323bf215546Sopenharmony_ci * VkSurfaceCapabilitiesKHR:supportedUsageFlags causes tests 324bf215546Sopenharmony_ci * dEQP-VK.wsi.android.swapchain.*.image_usage to fail. 325bf215546Sopenharmony_ci */ 326bf215546Sopenharmony_ci 327bf215546Sopenharmony_ci const VkPhysicalDeviceImageFormatInfo2 image_format_info = { 328bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2, 329bf215546Sopenharmony_ci .format = format, 330bf215546Sopenharmony_ci .type = VK_IMAGE_TYPE_2D, 331bf215546Sopenharmony_ci .tiling = VK_IMAGE_TILING_OPTIMAL, 332bf215546Sopenharmony_ci .usage = imageUsage, 333bf215546Sopenharmony_ci }; 334bf215546Sopenharmony_ci 335bf215546Sopenharmony_ci VkImageFormatProperties2 image_format_props = { 336bf215546Sopenharmony_ci .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2, 337bf215546Sopenharmony_ci }; 338bf215546Sopenharmony_ci 339bf215546Sopenharmony_ci /* Check that requested format and usage are supported. */ 340bf215546Sopenharmony_ci result = tu_GetPhysicalDeviceImageFormatProperties2( 341bf215546Sopenharmony_ci phys_dev_h, &image_format_info, &image_format_props); 342bf215546Sopenharmony_ci if (result != VK_SUCCESS) { 343bf215546Sopenharmony_ci return vk_errorf(device, result, 344bf215546Sopenharmony_ci "tu_GetPhysicalDeviceImageFormatProperties2 failed " 345bf215546Sopenharmony_ci "inside %s", 346bf215546Sopenharmony_ci __func__); 347bf215546Sopenharmony_ci } 348bf215546Sopenharmony_ci 349bf215546Sopenharmony_ci return VK_SUCCESS; 350bf215546Sopenharmony_ci} 351bf215546Sopenharmony_ci 352bf215546Sopenharmony_cistatic VkResult 353bf215546Sopenharmony_cisetup_gralloc0_usage(struct tu_device *device, VkFormat format, 354bf215546Sopenharmony_ci VkImageUsageFlags imageUsage, int *grallocUsage) 355bf215546Sopenharmony_ci{ 356bf215546Sopenharmony_ci if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_DST_BIT | 357bf215546Sopenharmony_ci VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) 358bf215546Sopenharmony_ci *grallocUsage |= GRALLOC_USAGE_HW_RENDER; 359bf215546Sopenharmony_ci 360bf215546Sopenharmony_ci if (unmask32(&imageUsage, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | 361bf215546Sopenharmony_ci VK_IMAGE_USAGE_SAMPLED_BIT | 362bf215546Sopenharmony_ci VK_IMAGE_USAGE_STORAGE_BIT | 363bf215546Sopenharmony_ci VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) 364bf215546Sopenharmony_ci *grallocUsage |= GRALLOC_USAGE_HW_TEXTURE; 365bf215546Sopenharmony_ci 366bf215546Sopenharmony_ci /* All VkImageUsageFlags not explicitly checked here are unsupported for 367bf215546Sopenharmony_ci * gralloc swapchains. 368bf215546Sopenharmony_ci */ 369bf215546Sopenharmony_ci if (imageUsage != 0) { 370bf215546Sopenharmony_ci return vk_errorf(device, VK_ERROR_FORMAT_NOT_SUPPORTED, 371bf215546Sopenharmony_ci "unsupported VkImageUsageFlags(0x%x) for gralloc " 372bf215546Sopenharmony_ci "swapchain", 373bf215546Sopenharmony_ci imageUsage); 374bf215546Sopenharmony_ci } 375bf215546Sopenharmony_ci 376bf215546Sopenharmony_ci /* 377bf215546Sopenharmony_ci * FINISHME: Advertise all display-supported formats. Mostly 378bf215546Sopenharmony_ci * DRM_FORMAT_ARGB2101010 and DRM_FORMAT_ABGR2101010, but need to check 379bf215546Sopenharmony_ci * what we need for 30-bit colors. 380bf215546Sopenharmony_ci */ 381bf215546Sopenharmony_ci if (format == VK_FORMAT_B8G8R8A8_UNORM || 382bf215546Sopenharmony_ci format == VK_FORMAT_B5G6R5_UNORM_PACK16) { 383bf215546Sopenharmony_ci *grallocUsage |= GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_COMPOSER | 384bf215546Sopenharmony_ci GRALLOC_USAGE_EXTERNAL_DISP; 385bf215546Sopenharmony_ci } 386bf215546Sopenharmony_ci 387bf215546Sopenharmony_ci if (*grallocUsage == 0) 388bf215546Sopenharmony_ci return VK_ERROR_FORMAT_NOT_SUPPORTED; 389bf215546Sopenharmony_ci 390bf215546Sopenharmony_ci return VK_SUCCESS; 391bf215546Sopenharmony_ci} 392bf215546Sopenharmony_ci 393bf215546Sopenharmony_ciVKAPI_ATTR VkResult VKAPI_CALL 394bf215546Sopenharmony_citu_GetSwapchainGrallocUsageANDROID(VkDevice device_h, 395bf215546Sopenharmony_ci VkFormat format, 396bf215546Sopenharmony_ci VkImageUsageFlags imageUsage, 397bf215546Sopenharmony_ci int *grallocUsage) 398bf215546Sopenharmony_ci{ 399bf215546Sopenharmony_ci TU_FROM_HANDLE(tu_device, device, device_h); 400bf215546Sopenharmony_ci VkResult result; 401bf215546Sopenharmony_ci 402bf215546Sopenharmony_ci result = format_supported_with_usage(device_h, format, imageUsage); 403bf215546Sopenharmony_ci if (result != VK_SUCCESS) 404bf215546Sopenharmony_ci return result; 405bf215546Sopenharmony_ci 406bf215546Sopenharmony_ci *grallocUsage = 0; 407bf215546Sopenharmony_ci return setup_gralloc0_usage(device, format, imageUsage, grallocUsage); 408bf215546Sopenharmony_ci} 409bf215546Sopenharmony_ci 410bf215546Sopenharmony_ci#if ANDROID_API_LEVEL >= 26 411bf215546Sopenharmony_ciVKAPI_ATTR VkResult VKAPI_CALL 412bf215546Sopenharmony_citu_GetSwapchainGrallocUsage2ANDROID(VkDevice device_h, 413bf215546Sopenharmony_ci VkFormat format, 414bf215546Sopenharmony_ci VkImageUsageFlags imageUsage, 415bf215546Sopenharmony_ci VkSwapchainImageUsageFlagsANDROID swapchainImageUsage, 416bf215546Sopenharmony_ci uint64_t *grallocConsumerUsage, 417bf215546Sopenharmony_ci uint64_t *grallocProducerUsage) 418bf215546Sopenharmony_ci{ 419bf215546Sopenharmony_ci TU_FROM_HANDLE(tu_device, device, device_h); 420bf215546Sopenharmony_ci VkResult result; 421bf215546Sopenharmony_ci 422bf215546Sopenharmony_ci *grallocConsumerUsage = 0; 423bf215546Sopenharmony_ci *grallocProducerUsage = 0; 424bf215546Sopenharmony_ci mesa_logd("%s: format=%d, usage=0x%x", __func__, format, imageUsage); 425bf215546Sopenharmony_ci 426bf215546Sopenharmony_ci result = format_supported_with_usage(device_h, format, imageUsage); 427bf215546Sopenharmony_ci if (result != VK_SUCCESS) 428bf215546Sopenharmony_ci return result; 429bf215546Sopenharmony_ci 430bf215546Sopenharmony_ci int32_t grallocUsage = 0; 431bf215546Sopenharmony_ci result = setup_gralloc0_usage(device, format, imageUsage, &grallocUsage); 432bf215546Sopenharmony_ci if (result != VK_SUCCESS) 433bf215546Sopenharmony_ci return result; 434bf215546Sopenharmony_ci 435bf215546Sopenharmony_ci /* Setup gralloc1 usage flags from gralloc0 flags. */ 436bf215546Sopenharmony_ci 437bf215546Sopenharmony_ci if (grallocUsage & GRALLOC_USAGE_HW_RENDER) { 438bf215546Sopenharmony_ci *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET; 439bf215546Sopenharmony_ci *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET; 440bf215546Sopenharmony_ci } 441bf215546Sopenharmony_ci 442bf215546Sopenharmony_ci if (grallocUsage & GRALLOC_USAGE_HW_TEXTURE) { 443bf215546Sopenharmony_ci *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE; 444bf215546Sopenharmony_ci } 445bf215546Sopenharmony_ci 446bf215546Sopenharmony_ci if (grallocUsage & (GRALLOC_USAGE_HW_FB | 447bf215546Sopenharmony_ci GRALLOC_USAGE_HW_COMPOSER | 448bf215546Sopenharmony_ci GRALLOC_USAGE_EXTERNAL_DISP)) { 449bf215546Sopenharmony_ci *grallocProducerUsage |= GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET; 450bf215546Sopenharmony_ci *grallocConsumerUsage |= GRALLOC1_CONSUMER_USAGE_HWCOMPOSER; 451bf215546Sopenharmony_ci } 452bf215546Sopenharmony_ci 453bf215546Sopenharmony_ci return VK_SUCCESS; 454bf215546Sopenharmony_ci} 455bf215546Sopenharmony_ci#endif 456