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 #ifndef OHOS_ANI_DRAWING_UTILS_H
17 #define OHOS_ANI_DRAWING_UTILS_H
18
19 #include <ani.h>
20 #include <hilog/log.h>
21 #include <iostream>
22 #include <memory>
23 #include <string>
24 #include "effect/color_filter.h"
25 #include "utils/rect.h"
26
27 #ifdef ROSEN_OHOS
28
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN 0xD001400
31
32 #undef LOG_TAG
33 #define LOG_TAG "AniDrawing"
34
35 #define ROSEN_LOGI(format, ...) \
36 HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__)
37 #define ROSEN_LOGD(format, ...) \
38 HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__)
39 #define ROSEN_LOGE(format, ...) \
40 HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__)
41 #else
42 #define ROSEN_LOGI(format, ...)
43 #define ROSEN_LOGD(format, ...)
44 #define ROSEN_LOGE(format, ...)
45 #endif
46
47 namespace OHOS {
48 namespace Rosen {
49 namespace Drawing {
50
51 constexpr char NATIVE_OBJ[] = "nativeObj";
52
53 ani_status AniThrowError(ani_env* env, const std::string& message);
54
CreateAniString(ani_env * env,const std::string & stdStr)55 inline ani_string CreateAniString(ani_env* env, const std::string& stdStr)
56 {
57 ani_string aniString;
58 env->String_NewUTF8(stdStr.c_str(), stdStr.size(), &aniString);
59 return aniString;
60 }
61
CreateStdString(ani_env * env,ani_string aniStr)62 inline std::string CreateStdString(ani_env* env, ani_string aniStr)
63 {
64 ani_size aniStrSize = 0;
65 env->String_GetUTF8Size(aniStr, &aniStrSize);
66 std::unique_ptr<char[]> buffer = std::make_unique<char[]>(aniStrSize + 1);
67 ani_size byteSize = 0;
68 env->String_GetUTF8(aniStr, buffer.get(), aniStrSize + 1, &byteSize);
69 buffer[byteSize] = '\0';
70 return std::string(buffer.get());
71 }
72
73 template<typename T>
GetNativeFromObj(ani_env * env,ani_object obj)74 T* GetNativeFromObj(ani_env* env, ani_object obj)
75 {
76 ani_long nativeObj {};
77 if (env->Object_GetFieldByName_Long(obj, NATIVE_OBJ, &nativeObj) != ANI_OK) {
78 ROSEN_LOGE("[ANI] Object_GetField_Long fetch failed");
79 return nullptr;
80 }
81 T *object = reinterpret_cast<T*>(nativeObj);
82 if (!object) {
83 ROSEN_LOGE("[ANI] object is null");
84 return nullptr;
85 }
86 return object;
87 }
88
89 ani_object CreateAniUndefined(ani_env* env);
90
91 ani_object CreateAniObject(ani_env* env, const char* className, const char* methodSig, ...);
92
93 template<typename T>
CreateAniObjectStatic(ani_env * env,const char * className,T * obj)94 ani_object CreateAniObjectStatic(ani_env* env, const char* className, T* obj)
95 {
96 if (obj == nullptr) {
97 ROSEN_LOGE("[Drawing] CreateAniObjectStatic obj is nullptr");
98 return CreateAniUndefined(env);
99 }
100 ani_class aniClass;
101 if (env->FindClass(className, &aniClass) != ANI_OK) {
102 ROSEN_LOGE("[Drawing] CreateAniObjectStatic FindClass failed");
103 delete obj;
104 return CreateAniUndefined(env);
105 }
106
107 ani_method aniConstructor;
108 if (env->Class_FindMethod(aniClass, "<ctor>", nullptr, &aniConstructor) != ANI_OK) {
109 ROSEN_LOGE("[Drawing] CreateAniObjectStatic Class_FindMethod constructor failed");
110 delete obj;
111 return CreateAniUndefined(env);
112 }
113
114 ani_object aniObject;
115 if (env->Object_New(aniClass, aniConstructor, &aniObject) != ANI_OK) {
116 ROSEN_LOGE("[Drawing] CreateAniObjectStatic Object_New failed");
117 delete obj;
118 return CreateAniUndefined(env);
119 }
120
121 ani_method innerMethod;
122 if (env->Class_FindMethod(aniClass, "bindNativePtr", "J:V", &innerMethod) != ANI_OK) {
123 ROSEN_LOGE("[Drawing] CreateAniObjectStatic Class_FindMethod bindNativePtr failed");
124 delete obj;
125 return CreateAniUndefined(env);
126 }
127 if (env->Object_CallMethod_Void(aniObject, innerMethod, reinterpret_cast<ani_long>(obj)) != ANI_OK) {
128 ROSEN_LOGE("[Drawing] CreateAniObjectStatic Object_CallMethod_Void failed");
129 delete obj;
130 return CreateAniUndefined(env);
131 }
132
133 return aniObject;
134 }
135
136 bool GetColorQuadFromParam(ani_env* env, ani_object obj, Drawing::ColorQuad &color);
137
138 bool GetColorQuadFromColorObj(ani_env* env, ani_object obj, Drawing::ColorQuad &color);
139
140 bool GetRectFromAniRectObj(ani_env* env, ani_object obj, Drawing::Rect& rect);
141
CheckDoubleOutOfRange(ani_double val,double lowerBound,double upperBound)142 inline bool CheckDoubleOutOfRange(ani_double val, double lowerBound, double upperBound)
143 {
144 return val < lowerBound || val > upperBound;
145 }
146
CheckInt32OutOfRange(ani_int val,int32_t lowerBound,int32_t upperBound)147 inline bool CheckInt32OutOfRange(ani_int val, int32_t lowerBound, int32_t upperBound)
148 {
149 return val < lowerBound || val > upperBound;
150 }
151
152 } // namespace Drawing
153 } // namespace Rosen
154 } // namespace OHOS
155 #endif // OHOS_ANI_DRAWING_UTILS_H