!2622 revert pack errCode

Merge pull request !2622 from 姚星宇/revertPackErrCode
This commit is contained in:
openharmony_ci 2024-09-30 09:59:34 +00:00 committed by Gitee
commit 963a944251
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 52 additions and 65 deletions

View File

@ -108,7 +108,6 @@ struct ImagePackerAsyncContext {
int64_t packedSize = 0; int64_t packedSize = 0;
int fd = INVALID_FD; int fd = INVALID_FD;
ImagePackerError error; ImagePackerError error;
bool needReturnErrorCode = true;
}; };
struct PackingOption { struct PackingOption {
@ -205,6 +204,16 @@ static void CommonCallbackRoutine(napi_env env, ImagePackerAsyncContext* &connec
connect = nullptr; connect = nullptr;
} }
static void BuildMsgOnError(ImagePackerAsyncContext* ctx, bool assertion, const std::string msg)
{
if (ctx == nullptr || assertion) {
return;
}
IMAGE_LOGE("%{public}s", msg.c_str());
ctx->error.hasErrorCode = false;
ctx->error.msg = msg;
}
static void BuildMsgOnError(ImagePackerAsyncContext* ctx, bool assertion, static void BuildMsgOnError(ImagePackerAsyncContext* ctx, bool assertion,
const std::string msg, int32_t errorCode) const std::string msg, int32_t errorCode)
{ {
@ -212,7 +221,7 @@ static void BuildMsgOnError(ImagePackerAsyncContext* ctx, bool assertion,
return; return;
} }
IMAGE_LOGE("%{public}s", msg.c_str()); IMAGE_LOGE("%{public}s", msg.c_str());
ctx->error.hasErrorCode = ctx->needReturnErrorCode; ctx->error.hasErrorCode = true;
ctx->error.errorCode = errorCode; ctx->error.errorCode = errorCode;
ctx->error.msg = msg; ctx->error.msg = msg;
} }
@ -263,7 +272,7 @@ bool SetPicture(ImagePackerAsyncContext *context)
IMAGE_LOGD("ImagePacker set picture"); IMAGE_LOGD("ImagePacker set picture");
if (context->rPicture == nullptr) { if (context->rPicture == nullptr) {
BuildMsgOnError(context, context->rImageSource == nullptr, BuildMsgOnError(context, context->rImageSource == nullptr,
"Picture is nullptr", COMMON_ERR_INVALID_PARAMETER); "Picture is nullptr", ERR_IMAGE_INVALID_PARAMETER);
return false; return false;
} }
context->rImagePacker->AddPicture(*(context->rPicture)); context->rImagePacker->AddPicture(*(context->rPicture));
@ -275,7 +284,7 @@ bool SetArrayPixel(ImagePackerAsyncContext *context)
{ {
IMAGE_LOGD("ImagePacker set pixelmap array"); IMAGE_LOGD("ImagePacker set pixelmap array");
if (!context->rPixelMaps) { if (!context->rPixelMaps) {
BuildMsgOnError(context, !context->rPixelMaps, "Pixelmap is nullptr", COMMON_ERR_INVALID_PARAMETER); BuildMsgOnError(context, !context->rPixelMaps, "Pixelmap is nullptr", ERR_IMAGE_INVALID_PARAMETER);
return false; return false;
} }
for (auto &pixelMap : *context->rPixelMaps.get()) { for (auto &pixelMap : *context->rPixelMaps.get()) {
@ -292,8 +301,7 @@ STATIC_EXEC_FUNC(Packing)
context->resultBuffer = std::make_unique<uint8_t[]>( context->resultBuffer = std::make_unique<uint8_t[]>(
(context->resultBufferSize <= 0) ? getDefaultBufferSize(context) : context->resultBufferSize); (context->resultBufferSize <= 0) ? getDefaultBufferSize(context) : context->resultBufferSize);
if (context->resultBuffer == nullptr) { if (context->resultBuffer == nullptr) {
BuildMsgOnError(context, context->resultBuffer == nullptr, "ImagePacker buffer alloc error", BuildMsgOnError(context, context->resultBuffer == nullptr, "ImagePacker buffer alloc error");
ERR_IMAGE_ENCODE_FAILED);
return; return;
} }
context->rImagePacker->StartPacking(context->resultBuffer.get(), context->rImagePacker->StartPacking(context->resultBuffer.get(),
@ -301,16 +309,14 @@ STATIC_EXEC_FUNC(Packing)
if (context->packType == TYPE_IMAGE_SOURCE) { if (context->packType == TYPE_IMAGE_SOURCE) {
IMAGE_LOGI("ImagePacker set image source"); IMAGE_LOGI("ImagePacker set image source");
if (context->rImageSource == nullptr) { if (context->rImageSource == nullptr) {
BuildMsgOnError(context, context->rImageSource == nullptr, "ImageSource is nullptr", BuildMsgOnError(context, context->rImageSource == nullptr, "ImageSource is nullptr");
COMMON_ERR_INVALID_PARAMETER);
return; return;
} }
context->rImagePacker->AddImage(*(context->rImageSource)); context->rImagePacker->AddImage(*(context->rImageSource));
} else if (context->packType == TYPE_PIXEL_MAP) { } else if (context->packType == TYPE_PIXEL_MAP) {
IMAGE_LOGD("ImagePacker set pixelmap"); IMAGE_LOGD("ImagePacker set pixelmap");
if (context->rPixelMap == nullptr) { if (context->rPixelMap == nullptr) {
BuildMsgOnError(context, context->rImageSource == nullptr, "Pixelmap is nullptr", BuildMsgOnError(context, context->rImageSource == nullptr, "Pixelmap is nullptr");
COMMON_ERR_INVALID_PARAMETER);
return; return;
} }
context->rImagePacker->AddImage(*(context->rPixelMap)); context->rImagePacker->AddImage(*(context->rPixelMap));
@ -325,20 +331,14 @@ STATIC_EXEC_FUNC(Packing)
return; return;
} }
} }
auto packRes = context->rImagePacker->FinalizePacking(packedSize); context->rImagePacker->FinalizePacking(packedSize);
IMAGE_LOGD("packedSize=%{public}" PRId64, packedSize); IMAGE_LOGD("packedSize=%{public}" PRId64, packedSize);
if (packRes == SUCCESS) { if (packedSize > 0 && (packedSize < context->resultBufferSize)) {
context->packedSize = packedSize; context->packedSize = packedSize;
context->status = SUCCESS; context->status = SUCCESS;
} else if (packedSize == context->resultBufferSize) {
context->status = ERROR;
BuildMsgOnError(context, packRes == SUCCESS, "output buffer is not enough", ERR_IMAGE_TOO_LARGE);
IMAGE_LOGE("output buffer is not enough.");
} else { } else {
context->status = ERROR; context->status = ERROR;
BuildMsgOnError(context, packRes == SUCCESS, "packing failed", IMAGE_LOGE("Packing failed, packedSize outside size.");
packRes == ERR_IMAGE_INVALID_PARAMETER ? COMMON_ERR_INVALID_PARAMETER : packRes);
IMAGE_LOGE("packing failed, packRes is %{public}u", packRes);
} }
} }
@ -414,7 +414,6 @@ napi_value ImagePackerNapi::Init(napi_env env, napi_value exports)
{ {
napi_property_descriptor props[] = { napi_property_descriptor props[] = {
DECLARE_NAPI_FUNCTION("packing", Packing), DECLARE_NAPI_FUNCTION("packing", Packing),
DECLARE_NAPI_FUNCTION("packToData", PackToData),
DECLARE_NAPI_FUNCTION("packToFile", PackToFile), DECLARE_NAPI_FUNCTION("packToFile", PackToFile),
DECLARE_NAPI_FUNCTION("packingFromPixelMap", Packing), DECLARE_NAPI_FUNCTION("packingFromPixelMap", Packing),
DECLARE_NAPI_FUNCTION("release", Release), DECLARE_NAPI_FUNCTION("release", Release),
@ -767,38 +766,37 @@ static void ParserPackingArguments(napi_env env,
{ {
int32_t refCount = 1; int32_t refCount = 1;
if (argc < PARAM1 || argc > PARAM3) { if (argc < PARAM1 || argc > PARAM3) {
BuildMsgOnError(context, (argc < PARAM1 || argc > PARAM3), "Arguments Count error", BuildMsgOnError(context, (argc < PARAM1 || argc > PARAM3), "Arguments Count error");
COMMON_ERR_INVALID_PARAMETER);
} }
context->packType = ParserPackingArgumentType(env, argv[PARAM0]); context->packType = ParserPackingArgumentType(env, argv[PARAM0]);
if (context->packType == TYPE_IMAGE_SOURCE) { if (context->packType == TYPE_IMAGE_SOURCE) {
context->rImageSource = GetImageSourceFromNapi(env, argv[PARAM0]); context->rImageSource = GetImageSourceFromNapi(env, argv[PARAM0]);
BuildMsgOnError(context, context->rImageSource != nullptr, "ImageSource mismatch", BuildMsgOnError(context, context->rImageSource != nullptr, "ImageSource mismatch");
COMMON_ERR_INVALID_PARAMETER);
} else if (context->packType == TYPE_PIXEL_MAP) { } else if (context->packType == TYPE_PIXEL_MAP) {
context->rPixelMap = PixelMapNapi::GetPixelMap(env, argv[PARAM0]); context->rPixelMap = PixelMapNapi::GetPixelMap(env, argv[PARAM0]);
BuildMsgOnError(context, context->rPixelMap != nullptr, "PixelMap mismatch", COMMON_ERR_INVALID_PARAMETER); BuildMsgOnError(context, context->rPixelMap != nullptr, "PixelMap mismatch");
#if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
} else if (context->packType == TYPE_PICTURE) { } else if (context->packType == TYPE_PICTURE) {
context->rPicture = PictureNapi::GetPicture(env, argv[PARAM0]); context->rPicture = PictureNapi::GetPicture(env, argv[PARAM0]);
BuildMsgOnError(context, context->rPicture != nullptr, "Picture mismatch", COMMON_ERR_INVALID_PARAMETER); BuildMsgOnError(context, context->rPicture != nullptr, "Picture mismatch");
#endif #endif
} else if (context->packType == TYPE_ARRAY) { } else if (context->packType == TYPE_ARRAY) {
context->rPixelMaps = PixelMapNapi::GetPixelMaps(env, argv[PARAM0]); context->rPixelMaps = PixelMapNapi::GetPixelMaps(env, argv[PARAM0]);
BuildMsgOnError(context, context->rPixelMaps != nullptr, "PixelMap mismatch", COMMON_ERR_INVALID_PARAMETER); BuildMsgOnError(context, context->rPixelMaps != nullptr,
"PixelMap mismatch", ERR_IMAGE_INVALID_PARAMETER);
} }
if (argc > PARAM1 && ImageNapiUtils::getType(env, argv[PARAM1]) == napi_object) { if (argc > PARAM1 && ImageNapiUtils::getType(env, argv[PARAM1]) == napi_object) {
if (context->packType == TYPE_ARRAY) { if (context->packType == TYPE_ARRAY) {
BuildMsgOnError(context, BuildMsgOnError(context,
parsePackOptionOfLoop(env, argv[PARAM1], &(context->packOption)), parsePackOptionOfLoop(env, argv[PARAM1], &(context->packOption)),
"PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); "PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER);
context->resultBufferSize = parseBufferSize(env, argv[PARAM1]); context->resultBufferSize = parseBufferSize(env, argv[PARAM1]);
BuildMsgOnError(context, BuildMsgOnError(context,
parsePackOptionOfdisposalTypes(env, argv[PARAM1], &(context->packOption)), parsePackOptionOfdisposalTypes(env, argv[PARAM1], &(context->packOption)),
"PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); "PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER);
} else { } else {
BuildMsgOnError(context, parsePackOptions(env, argv[PARAM1], &(context->packOption)), BuildMsgOnError(context,
"PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); parsePackOptions(env, argv[PARAM1], &(context->packOption)), "PackOptions mismatch");
context->resultBufferSize = parseBufferSize(env, argv[PARAM1], context); context->resultBufferSize = parseBufferSize(env, argv[PARAM1], context);
} }
} }
@ -807,7 +805,7 @@ static void ParserPackingArguments(napi_env env,
} }
} }
napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info, bool needReturnError) napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info)
{ {
ImageTrace imageTrace("ImagePackerNapi::Packing"); ImageTrace imageTrace("ImagePackerNapi::Packing");
napi_status status; napi_status status;
@ -824,7 +822,6 @@ napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info, bool
std::unique_ptr<ImagePackerAsyncContext> asyncContext = std::make_unique<ImagePackerAsyncContext>(); std::unique_ptr<ImagePackerAsyncContext> asyncContext = std::make_unique<ImagePackerAsyncContext>();
status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->constructor_)); status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->constructor_));
NAPI_ASSERT(env, IMG_IS_READY(status, asyncContext->constructor_), "fail to unwrap constructor_"); NAPI_ASSERT(env, IMG_IS_READY(status, asyncContext->constructor_), "fail to unwrap constructor_");
asyncContext->needReturnErrorCode = needReturnError;
asyncContext->rImagePacker = asyncContext->constructor_->nativeImgPck; asyncContext->rImagePacker = asyncContext->constructor_->nativeImgPck;
ParserPackingArguments(env, argv, argc, asyncContext.get()); ParserPackingArguments(env, argv, argc, asyncContext.get());
@ -847,16 +844,6 @@ napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info, bool
return result; return result;
} }
napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info)
{
return Packing(env, info, false);
}
napi_value ImagePackerNapi::PackToData(napi_env env, napi_callback_info info)
{
return Packing(env, info, true);
}
napi_value ImagePackerNapi::GetSupportedFormats(napi_env env, napi_callback_info info) napi_value ImagePackerNapi::GetSupportedFormats(napi_env env, napi_callback_info info)
{ {
napi_value result = nullptr; napi_value result = nullptr;
@ -945,43 +932,45 @@ static void ParserPackToFileArguments(napi_env env,
{ {
int32_t refCount = 1; int32_t refCount = 1;
if (argc < PARAM1 || argc > PARAM4) { if (argc < PARAM1 || argc > PARAM4) {
BuildMsgOnError(context, (argc < PARAM1 || argc > PARAM4), "Arguments Count error", BuildMsgOnError(context, (argc < PARAM1 || argc > PARAM4),
COMMON_ERR_INVALID_PARAMETER); "Arguments Count error", ERR_IMAGE_INVALID_PARAMETER);
} }
context->packType = ParserPackingArgumentType(env, argv[PARAM0]); context->packType = ParserPackingArgumentType(env, argv[PARAM0]);
if (context->packType == TYPE_IMAGE_SOURCE) { if (context->packType == TYPE_IMAGE_SOURCE) {
context->rImageSource = GetImageSourceFromNapi(env, argv[PARAM0]); context->rImageSource = GetImageSourceFromNapi(env, argv[PARAM0]);
BuildMsgOnError(context, context->rImageSource != nullptr, "ImageSource mismatch", BuildMsgOnError(context, context->rImageSource != nullptr,
COMMON_ERR_INVALID_PARAMETER); "ImageSource mismatch", ERR_IMAGE_INVALID_PARAMETER);
} else if (context->packType == TYPE_PIXEL_MAP) { } else if (context->packType == TYPE_PIXEL_MAP) {
context->rPixelMap = PixelMapNapi::GetPixelMap(env, argv[PARAM0]); context->rPixelMap = PixelMapNapi::GetPixelMap(env, argv[PARAM0]);
BuildMsgOnError(context, context->rPixelMap != nullptr, "PixelMap mismatch", COMMON_ERR_INVALID_PARAMETER); BuildMsgOnError(context, context->rPixelMap != nullptr,
"PixelMap mismatch", ERR_IMAGE_INVALID_PARAMETER);
#if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
} else if (context->packType == TYPE_PICTURE) { } else if (context->packType == TYPE_PICTURE) {
context->rPicture = PictureNapi::GetPicture(env, argv[PARAM0]); context->rPicture = PictureNapi::GetPicture(env, argv[PARAM0]);
BuildMsgOnError(context, context->rPicture != nullptr, BuildMsgOnError(context, context->rPicture != nullptr,
"Picture mismatch", COMMON_ERR_INVALID_PARAMETER); "Picture mismatch", ERR_IMAGE_INVALID_PARAMETER);
#endif #endif
} else if (context->packType == TYPE_ARRAY) { } else if (context->packType == TYPE_ARRAY) {
context->rPixelMaps = PixelMapNapi::GetPixelMaps(env, argv[PARAM0]); context->rPixelMaps = PixelMapNapi::GetPixelMaps(env, argv[PARAM0]);
BuildMsgOnError(context, context->rPixelMaps != nullptr, BuildMsgOnError(context, context->rPixelMaps != nullptr,
"PixelMap mismatch", COMMON_ERR_INVALID_PARAMETER); "PixelMap mismatch", ERR_IMAGE_INVALID_PARAMETER);
} }
if (argc > PARAM1 && ImageNapiUtils::getType(env, argv[PARAM1]) == napi_number) { if (argc > PARAM1 && ImageNapiUtils::getType(env, argv[PARAM1]) == napi_number) {
BuildMsgOnError(context, (napi_get_value_int32(env, argv[PARAM1], &(context->fd)) == napi_ok && BuildMsgOnError(context, (napi_get_value_int32(env, argv[PARAM1], &(context->fd)) == napi_ok &&
context->fd > INVALID_FD), "fd mismatch", COMMON_ERR_INVALID_PARAMETER); context->fd > INVALID_FD), "fd mismatch", ERR_IMAGE_INVALID_PARAMETER);
} }
if (argc > PARAM2 && ImageNapiUtils::getType(env, argv[PARAM2]) == napi_object) { if (argc > PARAM2 && ImageNapiUtils::getType(env, argv[PARAM2]) == napi_object) {
if (context->packType == TYPE_ARRAY) { if (context->packType == TYPE_ARRAY) {
BuildMsgOnError(context, BuildMsgOnError(context,
parsePackOptionOfLoop(env, argv[PARAM2], &(context->packOption)), parsePackOptionOfLoop(env, argv[PARAM2], &(context->packOption)),
"PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); "PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER);
BuildMsgOnError(context, BuildMsgOnError(context,
parsePackOptionOfdisposalTypes(env, argv[PARAM2], &(context->packOption)), parsePackOptionOfdisposalTypes(env, argv[PARAM2], &(context->packOption)),
"PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); "PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER);
} else { } else {
BuildMsgOnError(context, parsePackOptions(env, argv[PARAM2], &(context->packOption)), BuildMsgOnError(context,
"PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); parsePackOptions(env, argv[PARAM2], &(context->packOption)),
"PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER);
} }
} }
if (argc > PARAM3 && ImageNapiUtils::getType(env, argv[PARAM3]) == napi_function) { if (argc > PARAM3 && ImageNapiUtils::getType(env, argv[PARAM3]) == napi_function) {
@ -994,27 +983,28 @@ STATIC_EXEC_FUNC(PackToFile)
int64_t packedSize = 0; int64_t packedSize = 0;
auto context = static_cast<ImagePackerAsyncContext*>(data); auto context = static_cast<ImagePackerAsyncContext*>(data);
if (context->fd <= INVALID_FD) { if (context->fd <= INVALID_FD) {
BuildMsgOnError(context, context->fd <= INVALID_FD, "ImagePacker invalid fd", COMMON_ERR_INVALID_PARAMETER); BuildMsgOnError(context, context->fd <= INVALID_FD,
"ImagePacker invalid fd", ERR_IMAGE_INVALID_PARAMETER);
return; return;
} }
auto startRes = context->rImagePacker->StartPacking(context->fd, context->packOption); auto startRes = context->rImagePacker->StartPacking(context->fd, context->packOption);
if (startRes != SUCCESS) { if (startRes != SUCCESS) {
context->status = ERROR; context->status = ERROR;
BuildMsgOnError(context, startRes == SUCCESS, "Start packing failed", BuildMsgOnError(context, startRes == SUCCESS, "Start packing failed", startRes);
startRes == ERR_IMAGE_INVALID_PARAMETER ? COMMON_ERR_INVALID_PARAMETER : startRes);
return; return;
} }
IMAGE_LOGD("ImagePacker packType is %{public}d", context->packType);
if (context->packType == TYPE_IMAGE_SOURCE) { if (context->packType == TYPE_IMAGE_SOURCE) {
IMAGE_LOGD("ImagePacker set image source");
if (!context->rImageSource) { if (!context->rImageSource) {
BuildMsgOnError(context, !context->rImageSource, "ImageSource is nullptr", COMMON_ERR_INVALID_PARAMETER); BuildMsgOnError(context, !context->rImageSource, "ImageSource is nullptr", ERR_IMAGE_INVALID_PARAMETER);
return; return;
} }
context->rImagePacker->AddImage(*(context->rImageSource)); context->rImagePacker->AddImage(*(context->rImageSource));
} else if (context->packType == TYPE_PIXEL_MAP) { } else if (context->packType == TYPE_PIXEL_MAP) {
IMAGE_LOGD("ImagePacker set pixelmap");
if (!context->rPixelMap) { if (!context->rPixelMap) {
BuildMsgOnError(context, !context->rImageSource, "Pixelmap is nullptr", COMMON_ERR_INVALID_PARAMETER); BuildMsgOnError(context, !context->rImageSource, "Pixelmap is nullptr", ERR_IMAGE_INVALID_PARAMETER);
return; return;
} }
context->rImagePacker->AddImage(*(context->rPixelMap)); context->rImagePacker->AddImage(*(context->rPixelMap));
@ -1036,8 +1026,7 @@ STATIC_EXEC_FUNC(PackToFile)
context->status = SUCCESS; context->status = SUCCESS;
} else { } else {
context->status = ERROR; context->status = ERROR;
BuildMsgOnError(context, packRes == SUCCESS, "PackedSize outside size", BuildMsgOnError(context, packRes == SUCCESS, "PackedSize outside size", packRes);
packRes == ERR_IMAGE_INVALID_PARAMETER ? COMMON_ERR_INVALID_PARAMETER : packRes);
IMAGE_LOGE("Packing failed, packedSize outside size."); IMAGE_LOGE("Packing failed, packedSize outside size.");
} }
} }

View File

@ -50,8 +50,6 @@ private:
static napi_value Constructor(napi_env env, napi_callback_info info); static napi_value Constructor(napi_env env, napi_callback_info info);
static void Destructor(napi_env env, void *nativeObject, void *finalize); static void Destructor(napi_env env, void *nativeObject, void *finalize);
static napi_value Packing(napi_env env, napi_callback_info info); static napi_value Packing(napi_env env, napi_callback_info info);
static napi_value PackToData(napi_env env, napi_callback_info info);
static napi_value Packing(napi_env env, napi_callback_info info, bool needReturnError);
static napi_value PackingFromPixelMap(napi_env env, napi_callback_info info); static napi_value PackingFromPixelMap(napi_env env, napi_callback_info info);
static napi_value PackToFile(napi_env env, napi_callback_info info); static napi_value PackToFile(napi_env env, napi_callback_info info);
static napi_value Release(napi_env env, napi_callback_info info); static napi_value Release(napi_env env, napi_callback_info info);