diff --git a/BUILD.gn b/BUILD.gn index e5091933dca5a9b3fa778611f1eb641cf0c09a9e..2c2705d87cfbd43f408a9674d362f43cb5876025 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -43,6 +43,15 @@ ohos_source_set("astc_encoder_static") { "//third_party/astc-encoder/Source/astcenc_weight_align.cpp", "//third_party/astc-encoder/Source/astcenc_weight_quant_xfer_tables.cpp", ] + if (defined(global_parts_info) && + (defined(global_parts_info.graphic_graphic_2d_ext) || + defined(global_parts_info.product_hmos_sdk_product_hmos_sdk))) { + defines = [ "ASTC_CUSTOMIZED_ENABLE" ] + } + if (defined(global_parts_info) && + defined(global_parts_info.product_hmos_sdk_product_hmos_sdk)) { + defines += [ "BUILD_HMOS_SDK" ] + } part_name = "astc-encoder" subsystem_name = "thirdparty" } diff --git a/Source/astcenc.h b/Source/astcenc.h index 0df7534d796b9b394f46373b81b419a1089be46b..ffce903e1c5134a867ade6ae81a5adc81ecfc3bc 100644 --- a/Source/astcenc.h +++ b/Source/astcenc.h @@ -175,9 +175,27 @@ #define ASTCENC_NEON 0 #endif +#ifdef ASTC_CUSTOMIZED_ENABLE +#if defined(_WIN32) && !defined(__CYGWIN__) +#define NOMINMAX +#include +#include +#else +#include +#endif + +#include +#include +#include +#include +#endif + enum QualityProfile { HIGH_QUALITY_PROFILE = 0, // default profile - HIGH_SPEED_PROFILE + HIGH_SPEED_PROFILE, +#ifdef ASTC_CUSTOMIZED_ENABLE + CUSTOMIZED_PROFILE +#endif }; static const int HIGH_SPEED_PROFILE_BLOCK_MODE = 67; // keep openSource type, example @@ -892,4 +910,159 @@ ASTCENC_PUBLIC astcenc_error astcenc_get_block_info( ASTCENC_PUBLIC const char* astcenc_get_error_string( astcenc_error status); +#ifdef ASTC_CUSTOMIZED_ENABLE +#if defined(_WIN32) && !defined(__CYGWIN__) +const LPCSTR g_astcCustomizedSo = "libastcCustomizedEncode.dll"; +#elif defined(BUILD_HMOS_SDK) +const std::string g_astcCustomizedSo = "libastcCustomizedEncode.so"; +#else +const std::string g_astcCustomizedSo = "/system/lib64/module/hms/graphic/libastcCustomizedEncode.z.so"; +#endif +using IsCustomizedBlockMode = bool (*)(const int); +using CustomizedMaxPartitions = int (*)(); +using CustomizedBlockMode = int (*)(); + +class AstcCustomizedSoManager +{ +public: + AstcCustomizedSoManager() + { + astcCustomizedSoOpened_ = false; + astcCustomizedSoHandle_ = nullptr; + isCustomizedBlockModeFunc_ = nullptr; + customizedMaxPartitionsFunc_ = nullptr; + customizedBlockModeFunc_ = nullptr; + } + ~AstcCustomizedSoManager() + { + if (!astcCustomizedSoOpened_ || astcCustomizedSoHandle_ == nullptr) + { + printf("astcenc customized so is not be opened when dlclose!\n"); + } +#if defined(_WIN32) && !defined(__CYGWIN__) + if (!FreeLibrary(astcCustomizedSoHandle_)) + { + printf("astc dll FreeLibrary failed: %s\n", g_astcCustomizedSo); + } +#else + if (dlclose(astcCustomizedSoHandle_) != 0) + { + printf("astcenc so dlclose failed: %s\n", g_astcCustomizedSo.c_str()); + } +#endif + } + IsCustomizedBlockMode isCustomizedBlockModeFunc_; + CustomizedMaxPartitions customizedMaxPartitionsFunc_; + CustomizedBlockMode customizedBlockModeFunc_; + bool LoadSutCustomizedSo() + { + if (!astcCustomizedSoOpened_) + { +#if defined(_WIN32) && !defined(__CYGWIN__) + if ((_access(g_astcCustomizedSo, 0) == -1)) + { + printf("astc customized dll(%s) is not found!\n", g_astcCustomizedSo); + return false; + } + astcCustomizedSoHandle_ = LoadLibrary(g_astcCustomizedSo); + if (astcCustomizedSoHandle_ == nullptr) + { + printf("astc libAstcCustomizedEnc LoadLibrary failed!\n"); + return false; + } + isCustomizedBlockModeFunc_ = + reinterpret_cast(GetProcAddress(astcCustomizedSoHandle_, + "IsCustomizedBlockMode")); + if (isCustomizedBlockModeFunc_ == nullptr) + { + printf("astc isCustomizedBlockModeFunc_ GetProcAddress failed!\n"); + if (!FreeLibrary(astcCustomizedSoHandle_)) + { + printf("astc isCustomizedBlockModeFunc_ FreeLibrary failed!\n"); + } + return false; + } + customizedMaxPartitionsFunc_ = + reinterpret_cast(GetProcAddress(astcCustomizedSoHandle_, + "CustomizedMaxPartitions")); + if (customizedMaxPartitionsFunc_ == nullptr) + { + printf("astc customizedMaxPartitionsFunc_ GetProcAddress failed!\n"); + if (!FreeLibrary(astcCustomizedSoHandle_)) + { + printf("astc customizedMaxPartitionsFunc_ FreeLibrary failed!\n"); + } + return false; + } + customizedBlockModeFunc_ = + reinterpret_cast(GetProcAddress(astcCustomizedSoHandle_, + "CustomizedBlockMode")); + if (customizedBlockModeFunc_ == nullptr) + { + printf("astc customizedBlockModeFunc_ GetProcAddress failed!\n"); + if (!FreeLibrary(astcCustomizedSoHandle_)) + { + printf("astc customizedBlockModeFunc_ FreeLibrary failed!\n"); + } + return false; + } + printf("astcenc customized dll load success: %s!\n", g_astcCustomizedSo); +#else + if (access(g_astcCustomizedSo.c_str(), F_OK) == -1) + { + printf("astc customized so(%s) is not found!\n", g_astcCustomizedSo.c_str()); + return false; + } + astcCustomizedSoHandle_ = dlopen(g_astcCustomizedSo.c_str(), 1); + if (astcCustomizedSoHandle_ == nullptr) + { + printf("astc libAstcCustomizedEnc dlopen failed!\n"); + return false; + } + isCustomizedBlockModeFunc_ = + reinterpret_cast(dlsym(astcCustomizedSoHandle_, + "IsCustomizedBlockMode")); + if (isCustomizedBlockModeFunc_ == nullptr) + { + printf("astc isCustomizedBlockModeFunc_ dlsym failed!\n"); + dlclose(astcCustomizedSoHandle_); + astcCustomizedSoHandle_ = nullptr; + return false; + } + customizedMaxPartitionsFunc_ = + reinterpret_cast(dlsym(astcCustomizedSoHandle_, + "CustomizedMaxPartitions")); + if (customizedMaxPartitionsFunc_ == nullptr) + { + printf("astc customizedMaxPartitionsFunc_ dlsym failed!\n"); + dlclose(astcCustomizedSoHandle_); + astcCustomizedSoHandle_ = nullptr; + return false; + } + customizedBlockModeFunc_ = + reinterpret_cast(dlsym(astcCustomizedSoHandle_, + "CustomizedBlockMode")); + if (customizedBlockModeFunc_ == nullptr) + { + printf("astc customizedBlockModeFunc_ dlsym failed!\n"); + dlclose(astcCustomizedSoHandle_); + astcCustomizedSoHandle_ = nullptr; + return false; + } + printf("astcenc customized so dlopen success: %s\n", g_astcCustomizedSo.c_str()); +#endif + astcCustomizedSoOpened_ = true; + } + return true; + } +private: + bool astcCustomizedSoOpened_; +#if defined(_WIN32) && !defined(__CYGWIN__) + HINSTANCE astcCustomizedSoHandle_; +#else + void *astcCustomizedSoHandle_; +#endif +}; +extern AstcCustomizedSoManager g_astcCustomizedSoManager; +#endif #endif diff --git a/Source/astcenc_block_sizes.cpp b/Source/astcenc_block_sizes.cpp index 3f395433cb17de21b46587bb7ecec502f759c5c0..9e53ff28730f819a82072b2c1df3902abba315d9 100644 --- a/Source/astcenc_block_sizes.cpp +++ b/Source/astcenc_block_sizes.cpp @@ -857,6 +857,18 @@ static void construct_block_size_descriptor_2d( { continue; } +#ifdef ASTC_CUSTOMIZED_ENABLE + if (!g_astcCustomizedSoManager.LoadSutCustomizedSo() || + g_astcCustomizedSoManager.isCustomizedBlockModeFunc_ == nullptr) + { + printf("astcenc customized so dlopen failed or isCustomizedBlockModeFunc_ is nullptr!\n"); + return; + } + if ((privateProfile == CUSTOMIZED_PROFILE) && (!g_astcCustomizedSoManager.isCustomizedBlockModeFunc_(i))) + { + continue; + } +#endif // Decode parameters unsigned int x_weights; unsigned int y_weights; diff --git a/Source/astcenc_compress_symbolic.cpp b/Source/astcenc_compress_symbolic.cpp index 2560f544faca7fe49ed13a0ac0c9b20fef0762ed..df55d053e690dc0ba068d29e142654917a3bdf77 100644 --- a/Source/astcenc_compress_symbolic.cpp +++ b/Source/astcenc_compress_symbolic.cpp @@ -25,6 +25,9 @@ #include "astcenc_diagnostic_trace.h" #include +#ifdef ASTC_CUSTOMIZED_ENABLE +AstcCustomizedSoManager g_astcCustomizedSoManager; +#endif /** * @brief Merge two planes of endpoints into a single vector. @@ -1204,7 +1207,27 @@ void compress_block( float block_is_la_scale = block_is_la ? 1.0f / 1.05f : 1.0f; bool block_skip_two_plane = false; - int max_partitions = (ctx.config.privateProfile == HIGH_SPEED_PROFILE) ? 1 : ctx.config.tune_partition_count_limit; + int max_partitions; + if (ctx.config.privateProfile == HIGH_SPEED_PROFILE) + { + max_partitions = 1; + } +#ifdef ASTC_CUSTOMIZED_ENABLE + else if (ctx.config.privateProfile == CUSTOMIZED_PROFILE) + { + if (!g_astcCustomizedSoManager.LoadSutCustomizedSo() || + g_astcCustomizedSoManager.customizedMaxPartitionsFunc_ == nullptr) + { + printf("astcenc customized so dlopen failed or customizedMaxPartitionsFunc_ is nullptr!\n"); + return; + } + max_partitions = g_astcCustomizedSoManager.customizedMaxPartitionsFunc_(); + } +#endif + else + { + max_partitions = ctx.config.tune_partition_count_limit; + } unsigned int requested_partition_indices[3] { ctx.config.tune_2partition_index_limit, @@ -1259,13 +1282,28 @@ void compress_block( } trace_add_data("exit", "quality hit"); - if (ctx.config.privateProfile == HIGH_SPEED_PROFILE) + if (ctx.config.privateProfile != HIGH_QUALITY_PROFILE) { scb.block_type = SYM_BTYPE_NONCONST; scb.partition_count = 1; scb.color_formats_matched = 0; scb.plane2_component = -1; - scb.block_mode = HIGH_SPEED_PROFILE_BLOCK_MODE; + if (ctx.config.privateProfile == HIGH_SPEED_PROFILE) + { + scb.block_mode = HIGH_SPEED_PROFILE_BLOCK_MODE; + } +#ifdef ASTC_CUSTOMIZED_ENABLE + else if (ctx.config.privateProfile == CUSTOMIZED_PROFILE) + { + if (!g_astcCustomizedSoManager.LoadSutCustomizedSo() || + g_astcCustomizedSoManager.customizedBlockModeFunc_ == nullptr) + { + printf("astcenc customized so dlopen failed or customizedBlockModeFunc_ is nullptr!\n"); + return; + } + scb.block_mode = g_astcCustomizedSoManager.customizedBlockModeFunc_(); + } +#endif scb.partition_index = 0; scb.quant_mode = QUANT_256; scb.color_formats[0] = 12; // color format is 12 when block mode is HIGH_SPEED_PROFILE_BLOCK_MODE @@ -1368,7 +1406,7 @@ void compress_block( // alpha is the most likely to be non-correlated if it is present in the data. for (int i = BLOCK_MAX_COMPONENTS - 1; i >= 0; i--) { - if (ctx.config.privateProfile == HIGH_SPEED_PROFILE) + if (ctx.config.privateProfile != HIGH_QUALITY_PROFILE) { break; } diff --git a/Source/astcenc_pick_best_endpoint_format.cpp b/Source/astcenc_pick_best_endpoint_format.cpp index 4d8b4f77d45e77e578b518abe60c24afda96a345..dbc12ec6a6bf9901fa5a13428441cb5f9b7cb86a 100644 --- a/Source/astcenc_pick_best_endpoint_format.cpp +++ b/Source/astcenc_pick_best_endpoint_format.cpp @@ -682,7 +682,7 @@ static float one_partition_find_best_combination_for_bitcount( for (int integer_count = 1; integer_count <= 4; integer_count++) { - if (privateProfile == HIGH_SPEED_PROFILE) + if (privateProfile != HIGH_QUALITY_PROFILE) { integer_count = 4; // constant 4 bit count for HIGH_SPEED_PROFILE mode } @@ -706,7 +706,7 @@ static float one_partition_find_best_combination_for_bitcount( int ql = quant_mode_table[best_integer_count + 1][bits_available]; best_quant_level = static_cast(ql); - if (privateProfile == HIGH_SPEED_PROFILE) // keep openSource code style + if (privateProfile != HIGH_QUALITY_PROFILE) // keep openSource code style { best_format = FMT_RGBA; } @@ -784,6 +784,7 @@ static void two_partitions_find_best_combination_for_every_quantization_and_inte * @return The output error for the best pairing. */ static float two_partitions_find_best_combination_for_bitcount( + unsigned int privateProfile, float best_combined_error[21][7], uint8_t best_combined_format[21][7][2], int bits_available, @@ -793,8 +794,13 @@ static float two_partitions_find_best_combination_for_bitcount( ) { int best_integer_count = 0; float best_integer_count_error = ERROR_CALC_DEFAULT; + int integer_count = 2; + if (privateProfile != HIGH_QUALITY_PROFILE) + { + integer_count = 8; // constant 8 bit count + } - for (int integer_count = 2; integer_count <= 8; integer_count++) + for (; integer_count <= 8; integer_count++) { // Compute the quantization level for a given number of integers and a given number of bits int quant_level = quant_mode_table[integer_count][bits_available]; @@ -1209,9 +1215,10 @@ unsigned int compute_ideal_endpoint_formats( } float error_of_best = two_partitions_find_best_combination_for_bitcount( - combined_best_error, formats_of_choice, qwt_bitcounts[i], - best_quant_levels[i], best_quant_levels_mod[i], - best_ep_formats[i]); + privateProfile, + combined_best_error, formats_of_choice, qwt_bitcounts[i], + best_quant_levels[i], best_quant_levels_mod[i], + best_ep_formats[i]); float total_error = error_of_best + qwt_errors[i]; errors_of_best_combination[i] = total_error;