diff --git a/loader/loader.c b/loader/loader.c index d95076fa805806dae0d7b5821c9ebf632d4a6112..c370f422367d9a3be9140026af2d241ee186aacf 100644 --- a/loader/loader.c +++ b/loader/loader.c @@ -74,6 +74,17 @@ // Generated file containing all the extension data #include "vk_loader_extensions.c" +#if defined(VK_USE_PLATFORM_OHOS) +#define OHOS_BUILD_IN_LAYER_LIST_TOP_NUM 1 +static const char* OHOS_BUILD_IN_LAYER_NAME_LIST_TOP[OHOS_BUILD_IN_LAYER_LIST_TOP_NUM] = { + "VK_LAYER_HUAWEI_iGraphics", +}; +#define OHOS_BUILD_IN_LAYER_LIST_BOTTOM_NUM 1 +static const char* OHOS_BUILD_IN_LAYER_NAME_LIST_BOTTOM[OHOS_BUILD_IN_LAYER_LIST_BOTTOM_NUM] = { + "VK_LAYER_OHOS_surface", +}; +#endif + struct loader_struct loader = {0}; struct activated_layer_info { @@ -4433,6 +4444,84 @@ VKAPI_ATTR void VKAPI_CALL loader_layer_destroy_device(VkDevice device, const Vk } } +#if defined(VK_USE_PLATFORM_OHOS) +VkResult move_layer_to_specific_index(const struct loader_instance *inst, + struct loader_pointer_layer_list* const layer_list, const char* target_layer_name, uint32_t target_index) { + if (inst == NULL) { + return VK_ERROR_OUT_OF_HOST_MEMORY; + } + if (layer_list == NULL || target_layer_name == NULL || + target_index >= layer_list->count || target_index < 0) { + loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "move layer failed"); + return VK_ERROR_OUT_OF_HOST_MEMORY; + } + + // find current layer + int32_t current_index = -1; + struct loader_layer_properties *target_layer_properties_ptr = NULL; + for (int32_t i = 0; i < layer_list->count; i++) { + struct loader_layer_properties *layer_prop = layer_list->list[i]; + if (strcmp(layer_prop->info.layerName, target_layer_name) == 0) { + current_index = i; + target_layer_properties_ptr = layer_prop; + loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "find %s on %d", target_layer_name, i); + break; + } + } + + if (current_index == -1) { + loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "not found %s", target_layer_name); + return VK_SUCCESS; + } + + struct loader_layer_properties **layer_prop_ptr_list = layer_list->list; + if (current_index > target_index) { + for (int32_t i = current_index; i > target_index; i--) { + layer_list->list[i] = layer_list->list[i - 1]; + } + } else if (current_index < target_index) { + for (int32_t i = current_index; i < target_index; i++) { + layer_list->list[i] = layer_list->list[i + 1]; + } + } else { + loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "%s has been the %d", + target_layer_name, target_index); + return VK_SUCCESS; + } + + layer_list->list[target_index] = target_layer_properties_ptr; + loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "move %s from %d to %d", + target_layer_name, current_index, target_index); + return VK_SUCCESS; +} + +VkResult sort_layer_for_ohos(const struct loader_instance *inst, struct loader_pointer_layer_list* const layer_list) { + if (inst == NULL || layer_list == NULL) { + return VK_ERROR_OUT_OF_HOST_MEMORY; + } + loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "sort layer for ohos"); + + for (int32_t i = 0; i < OHOS_BUILD_IN_LAYER_LIST_TOP_NUM; i++) { + loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "layer %s", OHOS_BUILD_IN_LAYER_NAME_LIST_TOP[i]); + VkResult res = move_layer_to_specific_index(inst, layer_list, OHOS_BUILD_IN_LAYER_NAME_LIST_TOP[i], i); + if (res != VK_SUCCESS) { + return res; + } + } + + for (int32_t i = 0; i < OHOS_BUILD_IN_LAYER_LIST_BOTTOM_NUM; i++) { + loader_log(inst, VULKAN_LOADER_INFO_BIT, 0, "blayer %s", OHOS_BUILD_IN_LAYER_NAME_LIST_BOTTOM[i]); + uint32_t target_index = layer_list->count - i - 1; + VkResult res = move_layer_to_specific_index(inst, + layer_list, OHOS_BUILD_IN_LAYER_NAME_LIST_BOTTOM[i], target_index); + if (res != VK_SUCCESS) { + return res; + } + } + return VK_SUCCESS; +} +#endif + // Given the list of layers to activate in the loader_instance // structure. This function will add a VkLayerInstanceCreateInfo // structure to the VkInstanceCreateInfo.pNext pointer. @@ -4487,6 +4576,13 @@ VkResult loader_create_instance_chain(const VkInstanceCreateInfo *pCreateInfo, c return VK_ERROR_OUT_OF_HOST_MEMORY; } +#if defined(VK_USE_PLATFORM_OHOS) + VkResult sort_layer_res = sort_layer_for_ohos(inst, &inst->expanded_activated_layer_list); + if (sort_layer_res != VK_SUCCESS) { + loader_log(inst, VULKAN_LOADER_ERROR_BIT, 0, "sort layer for ohos failed"); + } +#endif + // Create instance chain of enabled layers for (int32_t i = inst->expanded_activated_layer_list.count - 1; i >= 0; i--) { struct loader_layer_properties *layer_prop = inst->expanded_activated_layer_list.list[i];