Sublele
Sublele

Reputation: 46

Can't find memoryTypeIndex for hardware buffer of ImageReader with VK_MEMORY_PROPERTY_PROTECTED_BIT

Background:
In order to render with DRM video, I create an ImageReader with usage USAGE_PROTECTED_CONTENT, which provide a Surface for video to play with. Then acquire image from it for later rendering.

Acquire Image and get corresponding AHardwareBuffer and Desc:

Image image = AcquireLatestImage();
HardwareBuffer hardware_buffer = AcquireImageBuffer(image);  
AHardwareBuffer *hwbuffer = AHardwareBuffer_fromHardwareBuffer(jni::env(), hardware_buffer.object().getHandle());
AHardwareBuffer_Desc hardware_buffer_desc;
AHardwareBuffer_describe(hwbuffer, &hardware_buffer_desc);
// the hardware_buffer_desc.usage match the bit AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT

Get hardware buffer properties:

VkAndroidHardwareBufferFormatPropertiesANDROID formatProps{};
formatProps.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID;

VkAndroidHardwareBufferPropertiesANDROID bufferProps = {};
bufferProps.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID;
bufferProps.pNext = &formatProps;
funcGetAndroidHardwareBufferPropertiesANDROID(context_->GetDevice(), (AHardwareBuffer*)hardware_buffer, &bufferProps);
// Here I get externalFormat, ycbcr* and bufferProps.memoryTypeBits = 8.

Then, I create VkImage with VkExternalMemoryImageCreateInfo & VkExternalFormatANDROID in the pNext of VkImageCreateInfo, and set VkImageCreateInfo.flags = VK_IMAGE_CREATE_PROTECTED_BIT.

It works well till now.

The problem appears when try to find a memory type index for device memory allocation.

My find function is:

uint32_t FindMemoryType(VkPhysicalDevice physical_device, uint32_t typeFilter, VkMemoryPropertyFlags properties) {
    VkPhysicalDeviceMemoryProperties memProperties;
    vkGetPhysicalDeviceMemoryProperties(physical_device, &memProperties);
    for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
        if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
                LOG_TRACE("FindMemoryType index : {}, properties: {}", i, properties)
            return i;
        }
    }
    throw std::runtime_error("failed to find suitable memory type!");
}

I called this function with parameters:

FindMemoryType(phyDevice, bufferProps.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_PROTECTED_BIT);

It can't find a proper index and will throw the runtime_error.

I also list all the memProperties, and find that it contains:

index: 0 = propertyFlags: 1
index: 1 = propertyFlags: 11
index: 2 = propertyFlags: 15
index: 3 = propertyFlags: 1
index: 4 = propertyFlags: 7
index: 5 = propertyFlags: 33

None is compatible with typeFilter = 8 and properties = 33.

Is there any other solution can do the same thing? Or am I do something wrong?

Upvotes: 0

Views: 79

Answers (0)

Related Questions