Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7360f8cb authored by Trevor Black's avatar Trevor Black Committed by Automerger Merge Worker
Browse files

Merge "Vulkan loader changes to support VK_EXT_image_compression_control" am: 7dbd7a6a

parents 4fbdec5b 7dbd7a6a
Loading
Loading
Loading
Loading
+42 −4
Original line number Diff line number Diff line
@@ -49,7 +49,13 @@ extern "C" {
 * in VkBindImageMemorySwapchainInfoKHR will be additionally chained to the
 * pNext chain of VkBindImageMemoryInfo and passed down to the driver.
 */
#define VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 8
/*
 * NOTE ON VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 9
 *
 * This version of the extension is largely designed to clean up the mix of
 * GrallocUsage and GrallocUsage2
 */
#define VK_ANDROID_NATIVE_BUFFER_SPEC_VERSION 9
#define VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME "VK_ANDROID_native_buffer"

#define VK_ANDROID_NATIVE_BUFFER_ENUM(type, id) \
@@ -61,6 +67,8 @@ extern "C" {
    VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 1)
#define VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID \
    VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 2)
#define VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID \
    VK_ANDROID_NATIVE_BUFFER_ENUM(VkStructureType, 3)

/* clang-format off */
typedef enum VkSwapchainImageUsageFlagBitsANDROID {
@@ -90,6 +98,7 @@ typedef struct {
 * format: gralloc format requested when the buffer was allocated
 * usage: gralloc usage requested when the buffer was allocated
 * usage2: gralloc usage requested when the buffer was allocated
 * usage3: gralloc usage requested when the buffer was allocated
 */
typedef struct {
    VkStructureType                   sType;
@@ -98,7 +107,8 @@ typedef struct {
    int                               stride;
    int                               format;
    int                               usage; /* DEPRECATED in SPEC_VERSION 6 */
    VkNativeBufferUsage2ANDROID       usage2; /* ADDED in SPEC_VERSION 6 */
    VkNativeBufferUsage2ANDROID       usage2; /* DEPRECATED in SPEC_VERSION 9 */
    uint64_t                          usage3; /* ADDED in SPEC_VERSION 9 */
} VkNativeBufferANDROID;

/*
@@ -127,6 +137,21 @@ typedef struct {
    VkBool32                          sharedImage;
} VkPhysicalDevicePresentationPropertiesANDROID;

/*
 * struct VkGrallocUsageInfoANDROID
 *
 * sType: VK_STRUCTURE_TYPE_GRALLOC_USAGE_INFO_ANDROID
 * pNext: NULL or a pointer to a structure extending this structure
 * format: value specifying the format the image will be created with
 * imageUsage: bitmask of VkImageUsageFlagBits describing intended usage
 */
typedef struct {
    VkStructureType                   sType;
    const void*                       pNext;
    VkFormat                          format;
    VkImageUsageFlags                 imageUsage;
} VkGrallocUsageInfoANDROID;

/* DEPRECATED in SPEC_VERSION 6 */
typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsageANDROID)(
    VkDevice                          device,
@@ -134,7 +159,7 @@ typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsageANDROID)(
    VkImageUsageFlags                 imageUsage,
    int*                              grallocUsage);

/* ADDED in SPEC_VERSION 6 */
/* DEPRECATED in SPEC_VERSION 9 */
typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage2ANDROID)(
    VkDevice                          device,
    VkFormat                          format,
@@ -143,6 +168,12 @@ typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage2ANDROID)(
    uint64_t*                         grallocConsumerUsage,
    uint64_t*                         grallocProducerUsage);

/* ADDED in SPEC_VERSION 9 */
typedef VkResult (VKAPI_PTR *PFN_vkGetSwapchainGrallocUsage3ANDROID)(
    VkDevice                          device,
    const VkGrallocUsageInfoANDROID*  grallocUsageInfo,
    uint64_t*                         grallocUsage);

typedef VkResult (VKAPI_PTR *PFN_vkAcquireImageANDROID)(
    VkDevice                          device,
    VkImage                           image,
@@ -167,7 +198,7 @@ VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsageANDROID(
    int*                              grallocUsage
);

/* ADDED in SPEC_VERSION 6 */
/* DEPRECATED in SPEC_VERSION 9 */
VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage2ANDROID(
    VkDevice                          device,
    VkFormat                          format,
@@ -177,6 +208,13 @@ VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage2ANDROID(
    uint64_t*                         grallocProducerUsage
);

/* ADDED in SPEC_VERSION 9 */
VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainGrallocUsage3ANDROID(
    VkDevice                          device,
    const VkGrallocUsageInfoANDROID*  grallocUsageInfo,
    uint64_t*                         grallocUsage
);

VKAPI_ATTR VkResult VKAPI_CALL vkAcquireImageANDROID(
    VkDevice                          device,
    VkImage                           image,
+147 −7
Original line number Diff line number Diff line
@@ -1027,6 +1027,39 @@ void QueryPresentationProperties(
    }
}

bool GetAndroidNativeBufferSpecVersion9Support(
    VkPhysicalDevice physicalDevice) {
    const InstanceData& data = GetData(physicalDevice);

    // Call to get propertyCount
    uint32_t propertyCount = 0;
    ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
    VkResult result = data.driver.EnumerateDeviceExtensionProperties(
        physicalDevice, nullptr, &propertyCount, nullptr);
    ATRACE_END();

    // Call to enumerate properties
    std::vector<VkExtensionProperties> properties(propertyCount);
    ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
    result = data.driver.EnumerateDeviceExtensionProperties(
        physicalDevice, nullptr, &propertyCount, properties.data());
    ATRACE_END();

    for (uint32_t i = 0; i < propertyCount; i++) {
        auto& prop = properties[i];

        if (strcmp(prop.extensionName,
                   VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
            continue;

        if (prop.specVersion >= 9) {
            return true;
        }
    }

    return false;
}

VkResult EnumerateDeviceExtensionProperties(
    VkPhysicalDevice physicalDevice,
    const char* pLayerName,
@@ -1061,6 +1094,37 @@ VkResult EnumerateDeviceExtensionProperties(
                VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
    }

    // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
    // support is provided by the driver
    VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
        swapchainCompFeats = {};
    swapchainCompFeats.sType =
        VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
    swapchainCompFeats.pNext = nullptr;
    VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
    imageCompFeats.sType =
        VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
    imageCompFeats.pNext = &swapchainCompFeats;

    VkPhysicalDeviceFeatures2 feats2 = {};
    feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
    feats2.pNext = &imageCompFeats;

    GetPhysicalDeviceFeatures2(physicalDevice, &feats2);

    bool anb9 = GetAndroidNativeBufferSpecVersion9Support(physicalDevice);

    if (anb9 && imageCompFeats.imageCompressionControl) {
        loader_extensions.push_back(
            {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
             VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
    }
    if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
        loader_extensions.push_back(
            {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
             VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
    }

    // enumerate our extensions first
    if (!pLayerName && pProperties) {
        uint32_t count = std::min(
@@ -1254,15 +1318,18 @@ VkResult CreateDevice(VkPhysicalDevice physicalDevice,
        return VK_ERROR_INCOMPATIBLE_DRIVER;
    }

    // sanity check ANDROID_native_buffer implementation, whose set of
    // Confirming ANDROID_native_buffer implementation, whose set of
    // entrypoints varies according to the spec version.
    if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
        !data->driver.GetSwapchainGrallocUsageANDROID &&
        !data->driver.GetSwapchainGrallocUsage2ANDROID) {
        ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
        !data->driver.GetSwapchainGrallocUsage2ANDROID &&
        !data->driver.GetSwapchainGrallocUsage3ANDROID) {
        ALOGE(
            "Driver's implementation of ANDROID_native_buffer is broken;"
            " must expose at least one of "
            "vkGetSwapchainGrallocUsageANDROID or "
              "vkGetSwapchainGrallocUsage2ANDROID");
            "vkGetSwapchainGrallocUsage2ANDROID or "
            "vkGetSwapchainGrallocUsage3ANDROID");

        data->driver.DestroyDevice(dev, pAllocator);
        FreeDeviceData(data, data_allocator);
@@ -1441,10 +1508,83 @@ void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,

    if (driver.GetPhysicalDeviceFeatures2) {
        driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
    } else {
        driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
    }

    // Conditionally add imageCompressionControlSwapchain if
    // imageCompressionControl is supported Check for imageCompressionControl in
    // the pChain
    bool imageCompressionControl = false;
    bool imageCompressionControlInChain = false;
    bool imageCompressionControlSwapchainInChain = false;
    VkPhysicalDeviceFeatures2* pFeats = pFeatures;
    while (pFeats) {
        switch (pFeats->sType) {
            case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
                const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
                    compressionFeat = reinterpret_cast<
                        const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
                        pFeats);
                imageCompressionControl =
                    compressionFeat->imageCompressionControl;
                imageCompressionControlInChain = true;
            } break;

            case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
                imageCompressionControlSwapchainInChain = true;
            } break;

            default:
                break;
        }
        pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
    }

    if (!imageCompressionControlSwapchainInChain) {
        return;
    }

    driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
    // If not in pchain, explicitly query for imageCompressionControl
    if (!imageCompressionControlInChain) {
        VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
        imageCompFeats.sType =
            VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
        imageCompFeats.pNext = nullptr;

        VkPhysicalDeviceFeatures2 feats2 = {};
        feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
        feats2.pNext = &imageCompFeats;

        if (driver.GetPhysicalDeviceFeatures2) {
            driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
        } else {
            driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
        }

        imageCompressionControl = imageCompFeats.imageCompressionControl;
    }

    // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
    if (imageCompressionControl) {
        pFeats = pFeatures;
        while (pFeats) {
            switch (pFeats->sType) {
                case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
                    VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
                        compressionFeat = reinterpret_cast<
                            VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
                            pFeats);
                    compressionFeat->imageCompressionControlSwapchain = true;
                } break;

                default:
                    break;
            }
            pFeats =
                reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
        }
    }
}

void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
+2 −0
Original line number Diff line number Diff line
@@ -107,6 +107,8 @@ void QueryPresentationProperties(
    VkPhysicalDevice physicalDevice,
    VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties);

bool GetAndroidNativeBufferSpecVersion9Support(VkPhysicalDevice physicalDevice);

VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance,
                                                  const char* pName);
VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device,
+8 −0
Original line number Diff line number Diff line
@@ -495,6 +495,13 @@ const ProcHook g_proc_hooks[] = {
        nullptr,
        nullptr,
    },
    {
        "vkGetSwapchainGrallocUsage3ANDROID",
        ProcHook::DEVICE,
        ProcHook::ANDROID_native_buffer,
        nullptr,
        nullptr,
    },
    {
        "vkGetSwapchainGrallocUsageANDROID",
        ProcHook::DEVICE,
@@ -664,6 +671,7 @@ bool InitDriverTable(VkDevice dev,
    INIT_PROC(false, dev, GetDeviceQueue2);
    INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsageANDROID);
    INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsage2ANDROID);
    INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsage3ANDROID);
    INIT_PROC_EXT(ANDROID_native_buffer, true, dev, AcquireImageANDROID);
    INIT_PROC_EXT(ANDROID_native_buffer, true, dev, QueueSignalReleaseImageANDROID);
    // clang-format on
+1 −0
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ struct DeviceDriverTable {
    PFN_vkGetDeviceQueue2 GetDeviceQueue2;
    PFN_vkGetSwapchainGrallocUsageANDROID GetSwapchainGrallocUsageANDROID;
    PFN_vkGetSwapchainGrallocUsage2ANDROID GetSwapchainGrallocUsage2ANDROID;
    PFN_vkGetSwapchainGrallocUsage3ANDROID GetSwapchainGrallocUsage3ANDROID;
    PFN_vkAcquireImageANDROID AcquireImageANDROID;
    PFN_vkQueueSignalReleaseImageANDROID QueueSignalReleaseImageANDROID;
    // clang-format on
Loading