From a5c7ce4ddaeb365311093487cc719e9c8df6c0d5 Mon Sep 17 00:00:00 2001 From: Joshua Duong Date: Thu, 14 Sep 2023 15:54:58 -0700 Subject: [PATCH] For goldfish pipe, compute colorBufferMemoryIndex the same as host. Reviewed-by: Aaron Ruby Acked-by: Yonggang Luo Acked-by: Adam Jackson Part-of: --- .../guest/vulkan_enc/ResourceTracker.cpp | 62 ++++++++++++++++--- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/src/gfxstream/guest/vulkan_enc/ResourceTracker.cpp b/src/gfxstream/guest/vulkan_enc/ResourceTracker.cpp index a9a8855cf0a..3ea4d0f3e51 100644 --- a/src/gfxstream/guest/vulkan_enc/ResourceTracker.cpp +++ b/src/gfxstream/guest/vulkan_enc/ResourceTracker.cpp @@ -1668,6 +1668,51 @@ public: } #ifdef VK_USE_PLATFORM_ANDROID_KHR + uint32_t getColorBufferMemoryIndex(void* context, VkDevice device) { + // Create test image to get the memory requirements + VkEncoder* enc = (VkEncoder*)context; + VkImageCreateInfo createInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .imageType = VK_IMAGE_TYPE_2D, + .format = VK_FORMAT_R8G8B8A8_UNORM, + .extent = {64, 64, 1}, + .mipLevels = 1, + .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | + VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT, + .initialLayout = VK_IMAGE_LAYOUT_MAX_ENUM, + }; + VkImage image = VK_NULL_HANDLE; + VkResult res = enc->vkCreateImage(device, &createInfo, nullptr, &image, true /* do lock */); + + if (res != VK_SUCCESS) { + return 0; + } + + VkMemoryRequirements memReqs; + enc->vkGetImageMemoryRequirements( + device, image, &memReqs, true /* do lock */); + enc->vkDestroyImage(device, image, nullptr, true /* do lock */); + + const VkPhysicalDeviceMemoryProperties& memProps = + getPhysicalDeviceMemoryProperties(context, device, VK_NULL_HANDLE); + + // Currently, host looks for the last index that has with memory + // property type VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT + VkMemoryPropertyFlags memoryProperty = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + for (int i = VK_MAX_MEMORY_TYPES - 1; i >= 0; --i) { + if ((memReqs.memoryTypeBits & (1u << i)) && + (memProps.memoryTypes[i].propertyFlags & memoryProperty)) { + return i; + } + } + + return 0; + } + VkResult on_vkGetAndroidHardwareBufferPropertiesANDROID( void* context, VkResult, VkDevice device, @@ -1677,12 +1722,9 @@ public: ResourceTracker::threadingCallbacks.hostConnectionGetFunc()->grallocHelper(); // Delete once goldfish Linux drivers are gone - if (mCaps.gfxstreamCapset.colorBufferMemoryIndex == 0xFFFFFFFF) { - const VkPhysicalDeviceMemoryProperties& memProps = - getPhysicalDeviceMemoryProperties(context, device, VK_NULL_HANDLE); - + if (mCaps.gfxstreamCapset.colorBufferMemoryIndex == 0xFFFFFFFF) { mCaps.gfxstreamCapset.colorBufferMemoryIndex = - (1u << memProps.memoryTypeCount) - 1; + getColorBufferMemoryIndex(context, device); } updateMemoryTypeBits(&pProperties->memoryTypeBits, @@ -4281,8 +4323,11 @@ public: // Delete `protocolVersion` check goldfish drivers are gone. #ifdef VK_USE_PLATFORM_ANDROID_KHR + if (mCaps.gfxstreamCapset.colorBufferMemoryIndex == 0xFFFFFFFF) { + mCaps.gfxstreamCapset.colorBufferMemoryIndex = + getColorBufferMemoryIndex(context, device); + } if (extImgCiPtr && - mCaps.gfxstreamCapset.protocolVersion && (extImgCiPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)) { updateMemoryTypeBits(&memReqs.memoryTypeBits, @@ -5337,8 +5382,11 @@ public: // Delete `protocolVersion` check goldfish drivers are gone. #ifdef VK_USE_PLATFORM_ANDROID_KHR + if (mCaps.gfxstreamCapset.colorBufferMemoryIndex == 0xFFFFFFFF) { + mCaps.gfxstreamCapset.colorBufferMemoryIndex = + getColorBufferMemoryIndex(context, device); + } if (extBufCiPtr && - mCaps.gfxstreamCapset.protocolVersion && (extBufCiPtr->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)) { updateMemoryTypeBits(&memReqs.memoryTypeBits,