1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ani_effect_kit_utils.h"
17 #include "effect_utils.h"
18
19 namespace OHOS {
20 namespace Rosen {
21
CreateAniObject(ani_env * env,const char * className,const char * methodSig,ani_long object)22 ani_object AniEffectKitUtils::CreateAniObject(
23 ani_env* env, const char* className, const char* methodSig, ani_long object)
24 {
25 ani_class cls;
26 if (env->FindClass(className, &cls) != ANI_OK) {
27 EFFECT_LOG_E("Not found class");
28 return AniEffectKitUtils::CreateAniUndefined(env);
29 }
30 ani_method ctor;
31 if (env->Class_FindMethod(cls, "<ctor>", methodSig, &ctor) != ANI_OK) {
32 EFFECT_LOG_E("Not found ani_method");
33 return AniEffectKitUtils::CreateAniUndefined(env);
34 }
35 ani_object aniValue;
36 if (env->Object_New(cls, ctor, &aniValue, object) != ANI_OK) {
37 EFFECT_LOG_E("New Context Failed");
38 return AniEffectKitUtils::CreateAniUndefined(env);
39 }
40 return aniValue;
41 }
42
CreateAniUndefined(ani_env * env)43 ani_object AniEffectKitUtils::CreateAniUndefined(ani_env* env)
44 {
45 ani_ref aniRef;
46 env->GetUndefined(&aniRef);
47 return static_cast<ani_object>(aniRef);
48 }
49
GetFilterFromEnv(ani_env * env,ani_object obj)50 AniFilter* AniEffectKitUtils::GetFilterFromEnv([[maybe_unused]] ani_env* env, [[maybe_unused]] ani_object obj)
51 {
52 ani_status ret;
53 ani_long nativeObj {};
54 if ((ret = env->Object_GetFieldByName_Long(obj, "nativeObj", &nativeObj)) != ANI_OK) {
55 EFFECT_LOG_E("Object_GetField_Long fetch failed, %{public}d", ret);
56 return nullptr;
57 }
58 AniFilter* aniFilter = reinterpret_cast<AniFilter*>(nativeObj);
59 if (!aniFilter) {
60 EFFECT_LOG_E("filter is null");
61 return nullptr;
62 }
63 return aniFilter;
64 }
65 } // namespace Rosen
66 } // namespace OHOS