1 /*
2  * Copyright (C) 2022 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 FRAMEWORKS_KITS_JS_COMMON_INCLUDE_IMAGE_NAPI_UTILS_H
17 #define FRAMEWORKS_KITS_JS_COMMON_INCLUDE_IMAGE_NAPI_UTILS_H
18 
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 
22 #define IMG_IS_OK(x) ((x) == napi_ok)
23 #define IMG_NOT_NULL(p) ((p) != nullptr)
24 #define IMG_IS_READY(x, p) (IMG_IS_OK(x) && IMG_NOT_NULL(p))
25 
26 #define IMG_NAPI_CHECK_RET_D(x, res, msg) \
27 do \
28 { \
29     if (!(x)) \
30     { \
31         msg; \
32         return (res); \
33     } \
34 } while (0)
35 
36 #define IMG_NAPI_CHECK_RET(x, res) \
37 do \
38 { \
39     if (!(x)) \
40     { \
41         return (res); \
42     } \
43 } while (0)
44 
45 #define IMG_JS_ARGS(env, info, status, argc, argv, thisVar) \
46 do \
47 { \
48     status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); \
49 } while (0)
50 
51 #define IMG_JS_NO_ARGS(env, info, status, thisVar) \
52 do \
53 { \
54     status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); \
55 } while (0)
56 
57 #define IMG_NAPI_CHECK_BUILD_ERROR(x, build, res, result) \
58 do \
59 { \
60     if (!(x)) \
61     { \
62         build; \
63         { \
64             res; \
65         } \
66         return (result); \
67     } \
68 } while (0)
69 
70 #define IMG_CREATE_CREATE_ASYNC_WORK(env, status, workName, exec, complete, aContext, work) \
71 do \
72 { \
73     napi_value _resource = nullptr; \
74     napi_create_string_utf8((env), (workName), NAPI_AUTO_LENGTH, &_resource); \
75     (status) = napi_create_async_work(env, nullptr, _resource, (exec), \
76             (complete), static_cast<void*>((aContext).get()), &(work)); \
77     if ((status) == napi_ok) { \
78         (status) = napi_queue_async_work((env), (work)); \
79         if ((status) == napi_ok) { \
80             (aContext).release(); \
81         } \
82     } \
83 } while (0)
84 
85 #define IMG_CREATE_CREATE_ASYNC_WORK_WITH_QOS(env, status, workName, exec, complete, aContext, work, qos) \
86 do \
87 { \
88     napi_value _resource = nullptr; \
89     napi_create_string_utf8((env), (workName), NAPI_AUTO_LENGTH, &_resource); \
90     (status) = napi_create_async_work(env, nullptr, _resource, (exec), \
91             (complete), static_cast<void*>((aContext).get()), &(work)); \
92     if ((status) == napi_ok) { \
93         (status) = napi_queue_async_work_with_qos((env), (work), (qos)); \
94         if ((status) == napi_ok) { \
95             (aContext).release(); \
96         } \
97     } \
98 } while (0)
99 
100 #define IMG_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
101 
102 #define GET_BUFFER_BY_NAME(root, name, res, len) ImageNapiUtils::GetBufferByName(env, (root), (name), &(res), &(len))
103 #define GET_UINT32_BY_NAME(root, name, res) ImageNapiUtils::GetUint32ByName(env, (root), (name), &(res))
104 #define GET_INT32_BY_NAME(root, name, res) ImageNapiUtils::GetInt32ByName(env, (root), (name), &(res))
105 #define GET_BOOL_BY_NAME(root, name, res) ImageNapiUtils::GetBoolByName(env, (root), (name), &(res))
106 #define GET_NODE_BY_NAME(root, name, res) ImageNapiUtils::GetNodeByName(env, (root), (name), &(res))
107 #define GET_DOUBLE_BY_NAME(root, name, res) ImageNapiUtils::GetDoubleByName(env, (root), (name), &(res))
108 
109 #define CREATE_NAPI_INT32(value, root) ImageNapiUtils::CreateNapiInt32(env, (value), (root))
110 #define CREATE_NAPI_DOUBLE(value, root) ImageNapiUtils::CreateNapiDouble(env, (value), (root))
111 
112 #define STATIC_EXEC_FUNC(name) static void name ## Exec(napi_env env, void *data)
113 #define STATIC_COMPLETE_FUNC(name) static void name ## Complete(napi_env env, napi_status status, void *data)
114 #define STATIC_NAPI_VALUE_FUNC(name) static napi_value name ## NapiValue(napi_env env, void *data, void *ptr)
115 
116 #define DECORATOR_HILOG(op, fmt, args...) \
117 do { \
118     op(LOG_CORE, fmt, ##args); \
119 } while (0)
120 
121 #define IMAGE_ERR(fmt, ...) DECORATOR_HILOG(HILOG_ERROR, fmt, ##__VA_ARGS__)
122 #ifdef IMAGE_DEBUG_FLAG
123 #define IMAGE_INFO(fmt, ...) DECORATOR_HILOG(HILOG_INFO, fmt, ##__VA_ARGS__)
124 #define IMAGE_DEBUG(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt, ##__VA_ARGS__)
125 #define IMAGE_FUNCTION_IN(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}s IN", ##__VA_ARGS__, __FUNCTION__)
126 #define IMAGE_FUNCTION_OUT(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}s OUT", ##__VA_ARGS__, __FUNCTION__)
127 #define IMAGE_LINE_IN(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}d IN", ##__VA_ARGS__, __LINE__)
128 #define IMAGE_LINE_OUT(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}d OUT", ##__VA_ARGS__, __LINE__)
129 #else
130 #define IMAGE_INFO(fmt, ...)
131 #define IMAGE_DEBUG(fmt, ...)
132 #define IMAGE_FUNCTION_IN(fmt, ...)
133 #define IMAGE_FUNCTION_OUT(fmt, ...)
134 #define IMAGE_LINE_IN(fmt, ...)
135 #define IMAGE_LINE_OUT(fmt, ...)
136 #endif
137 
138 namespace OHOS {
139 namespace Media {
140 class ImageNapiUtils {
141 public:
142     static bool GetBufferByName(napi_env env, napi_value root, const char* name, void **res, size_t* len);
143     static bool GetUint32ByName(napi_env env, napi_value root, const char* name, uint32_t *res);
144     static bool GetInt32ByName(napi_env env, napi_value root, const char* name, int32_t *res);
145     static bool GetDoubleByName(napi_env env, napi_value root, const char* name, double *res);
146     static bool GetBoolByName(napi_env env, napi_value root, const char* name, bool *res);
147     static bool GetNodeByName(napi_env env, napi_value root, const char* name, napi_value *res);
148     static bool GetUtf8String(napi_env env, napi_value root, std::string &res, bool eof = true);
149     static napi_valuetype getType(napi_env env, napi_value root);
150     static bool CreateArrayBuffer(napi_env env, void* src, size_t srcLen, napi_value *res);
151     static bool CreateNapiInt32(napi_env env, int32_t value, napi_value &root);
152     static bool CreateNapiDouble(napi_env env, double value, napi_value &root);
153     static bool ParseImageCreatorReceiverArgs(napi_env env, size_t argc,
154         napi_value argv[], int32_t args[], std::string &errMsg);
155     static void HicheckerReport();
156     static void CreateErrorObj(napi_env env, napi_value &errorObj,
157         const int32_t errCode, const std::string errMsg);
158     static napi_value ThrowExceptionError(napi_env env, const int32_t errCode, const std::string errMsg);
159 };
160 } // namespace Media
161 } // namespace OHOS
162 #endif // FRAMEWORKS_KITS_JS_COMMON_INCLUDE_IMAGE_NAPI_UTILS_H
163