How to export the vulkan image to android hardware buffer?

In detail, i want to write to android hardware buffer directly. But when i consulted the documentation specification of vulkan. In the following link, https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#memory-external-handle-types in chapter of 11.2.17, i found the specification said “Vulkan can import Android hardware buffers that have external formats, but since the image contents are in an undiscoverable and possibly proprietary representation, images with external formats must only be used as sampled images …”
so i change the point to create ta vulkan image that can be attached to frame buffer, then export that image to andorid hardware buffer. i create an export image like this.

void createExportImage()
{
    VkDevice device;
    ...
	VkExternalMemoryImageCreateInfo externalMemoryImageCreateInfo
	{
		VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
		nullptr,
		VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID
	};
	VkImageCreateInfo imageCreateInfo = {};
	imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
	imageCreateInfo.pNext = &externalMemoryImageCreateInfo;
	imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
	imageCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
	imageCreateInfo.extent.width = uint32_t(lWidth);
	imageCreateInfo.extent.height = uint32_t(lHeight);
	imageCreateInfo.extent.depth = 1;
	imageCreateInfo.mipLevels = mipLevels;
	imageCreateInfo.arrayLayers = 1;
	imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
	imageCreateInfo.queueFamilyIndexCount = 0;
	imageCreateInfo.pQueueFamilyIndices = nullptr;
	imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
	imageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
	imageCreateInfo.flags = 0;
	imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
	imageCreateInfo.tiling = VK_IMAGE_TILING_LINEAR;

	vkCreateImage(device, &imageCreateInfo, NULL, &image);

	VkMemoryRequirements memReqs;
	vkGetImageMemoryRequirements(device, image, &memReqs);

	VkExportMemoryAllocateInfo exportAllocInfo{};
	exportAllocInfo.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;

	VkMemoryAllocateInfo memAllocInfo = {};
	memAllocInfo.pNext = &exportAllocInfo;
	memAllocInfo.allocationSize = memReqs.size;
	memAllocInfo.memoryTypeIndex = 0;
	// Allocate the memory for buffer objects
	vkAllocateMemory(device, &memAllocInfo,nullptr, &memory);
	// Bind the image device memory 
	vkBindImageMemory(device, image, memory, 0);
}

then in android hardware buffer side, use the code like that:

bool SetVkDeviceMemory(AHardwareBuffer* buffer, VkDevice* pDevice, VkDeviceMemory* pVkDeviceMemory)
{
    VkMemoryGetAndroidHardwareBufferInfoANDROID memoryInfo
    {
        .sType = VK_STRUCTURE_TYPE_MEMORY_GET_ANDROID_HARDWARE_BUFFER_INFO_ANDROID,
        .pNext = nullptr,
        .memory = *pVkDeviceMemory,
    };
    vkGetMemoryAndroidHardwareBufferANDROID(*pDevice, &memoryInfo, &buffer);
    return true;
}

Now, that problem is that the export seems not work as expected.So what the entire process of export
vulkan image to android hardware buffer?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.