From 11d6c2086fcd90614d50d5fbc60f707c63fa089b Mon Sep 17 00:00:00 2001 From: zhaona45 Date: Fri, 11 Jul 2025 11:30:56 +0800 Subject: [PATCH] Add marshalling and CreatePictureFromParcel Signed-off-by: zhaona45 Change-Id: I06e83f11cbcd9ff9619c9f24700f3eefdee23710 --- .../kits/taihe/src/image_taihe_utils.cpp | 1 + frameworks/kits/taihe/src/picture_taihe.cpp | 37 +++++++------------ 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/frameworks/kits/taihe/src/image_taihe_utils.cpp b/frameworks/kits/taihe/src/image_taihe_utils.cpp index fd4443fb3..26c080903 100644 --- a/frameworks/kits/taihe/src/image_taihe_utils.cpp +++ b/frameworks/kits/taihe/src/image_taihe_utils.cpp @@ -54,6 +54,7 @@ ani_object ImageTaiheUtils::ToBusinessError(ani_env *env, int32_t code, const st { ani_object err {}; ani_class cls {}; + CHECK_ERROR_RETURN_RET_LOG(env == nullptr, nullptr, "get_env failed"); CHECK_ERROR_RETURN_RET_LOG(ANI_OK != env->FindClass(CLASS_NAME_BUSINESSERROR, &cls), err, "find class %{public}s failed", CLASS_NAME_BUSINESSERROR); ani_method ctor {}; diff --git a/frameworks/kits/taihe/src/picture_taihe.cpp b/frameworks/kits/taihe/src/picture_taihe.cpp index bdeacada6..9780775b6 100644 --- a/frameworks/kits/taihe/src/picture_taihe.cpp +++ b/frameworks/kits/taihe/src/picture_taihe.cpp @@ -245,30 +245,24 @@ Metadata PictureImpl::GetMetadataSync(MetadataType metadataType) return make_holder(imageMetadata); } -static bool UnwarpMessageParcel(const uintptr_t& sequence, ani_long& nativePtr) +static OHOS::MessageParcel* UnwarpMessageParcel(uintptr_t sequence) { - ani_env *env = ::taihe::get_env(); - if (env == nullptr) { - IMAGE_LOGE("%{pubilc}s Get ani env failed.", __func__); - return false; - } - ani_object sequenceObj = reinterpret_cast(sequence); - if (ANI_OK != env->Object_GetFieldByName_Long(sequenceObj, "nativePtr", &nativePtr)) { - IMAGE_LOGE("%{pubilc}s Get messageParcel nativePtr failed.", __func__); - return false; - } - return true; + ani_env* env = taihe::get_env(); + CHECK_ERROR_RETURN_RET_LOG(env == nullptr, nullptr, "get_env failed"); + ani_long messageParcel {}; + ani_status status = env->Object_CallMethodByName_Long(reinterpret_cast(sequence), "getNativePtr", + nullptr, &messageParcel); + if (status != ANI_OK) { + IMAGE_LOGE("UnwarpMessageParcel failed. status: %{public}d", status); + return nullptr; + } + return reinterpret_cast(messageParcel); } void PictureImpl::Marshalling(uintptr_t sequence) { #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) - ani_long nativePtr; - if (!UnwarpMessageParcel(sequence, nativePtr)) { - ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Marshalling picture unwrap failed."); - return; - } - OHOS::MessageParcel* messageParcel = reinterpret_cast(nativePtr); + OHOS::MessageParcel* messageParcel = UnwarpMessageParcel(sequence); if (messageParcel == nullptr) { ImageTaiheUtils::ThrowExceptionError(OHOS::Media::ERR_IPC, "Marshalling picture to parcel failed."); return; @@ -316,12 +310,7 @@ Picture CreatePictureFromParcel(uintptr_t sequence) { IMAGE_LOGD("Call CreatePictureFromParcel"); - ani_long nativePtr; - if (!UnwarpMessageParcel(sequence, nativePtr)) { - ImageTaiheUtils::ThrowExceptionError(IMAGE_BAD_PARAMETER, "Fail to unwrap messageSequence"); - return make_holder(); - } - OHOS::MessageParcel* messageParcel = reinterpret_cast(nativePtr); + OHOS::MessageParcel* messageParcel = UnwarpMessageParcel(sequence); if (messageParcel == nullptr) { ImageTaiheUtils::ThrowExceptionError(OHOS::Media::ERR_IPC, "Get parcel failed"); return make_holder(); -- Gitee