• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BUILD_ERROR(x, build, res, result) \
37 do \
38 { \
39     if (!(x)) \
40     { \
41         build; \
42         { \
43             res; \
44         } \
45         return (result); \
46     } \
47 } while (0)
48 
49 #define IMG_NAPI_CHECK_RET(x, res) \
50 do \
51 { \
52     if (!(x)) \
53     { \
54         return (res); \
55     } \
56 } while (0)
57 
58 #define IMG_JS_ARGS(env, info, status, argc, argv, thisVar) \
59 do \
60 { \
61     status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); \
62 } while (0)
63 
64 #define IMG_JS_NO_ARGS(env, info, status, thisVar) \
65 do \
66 { \
67     status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); \
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 NAPI_CHECK_AND_DELETE_REF(env, ref) \
101 do \
102 { \
103     if ((env) != nullptr && (ref) != nullptr) { \
104         napi_delete_reference((env), (ref)); \
105         ref = nullptr; \
106     } \
107 } while (0)
108 
109 #define IMG_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
110 
111 #define GET_BUFFER_BY_NAME(root, name, res, len) ImageNapiUtils::GetBufferByName(env, (root), (name), &(res), &(len))
112 #define GET_UINT32_BY_NAME(root, name, res) ImageNapiUtils::GetUint32ByName(env, (root), (name), &(res))
113 #define GET_INT32_BY_NAME(root, name, res) ImageNapiUtils::GetInt32ByName(env, (root), (name), &(res))
114 #define GET_BOOL_BY_NAME(root, name, res) ImageNapiUtils::GetBoolByName(env, (root), (name), &(res))
115 #define GET_NODE_BY_NAME(root, name, res) ImageNapiUtils::GetNodeByName(env, (root), (name), &(res))
116 #define GET_DOUBLE_BY_NAME(root, name, res) ImageNapiUtils::GetDoubleByName(env, (root), (name), &(res))
117 
118 #define CREATE_NAPI_INT32(value, root) ImageNapiUtils::CreateNapiInt32(env, (value), (root))
119 #define CREATE_NAPI_DOUBLE(value, root) ImageNapiUtils::CreateNapiDouble(env, (value), (root))
120 
121 #define STATIC_EXEC_FUNC(name) static void name ## Exec(napi_env env, void *data)
122 #define STATIC_COMPLETE_FUNC(name) static void name ## Complete(napi_env env, napi_status status, void *data)
123 #define STATIC_NAPI_VALUE_FUNC(name) static napi_value name ## NapiValue(napi_env env, void *data, void *ptr)
124 
125 #define DECORATOR_HILOG(op, fmt, args...) \
126 do { \
127     op(LOG_CORE, fmt, ##args); \
128 } while (0)
129 
130 #define IMAGE_ERR(fmt, ...) DECORATOR_HILOG(HILOG_ERROR, fmt, ##__VA_ARGS__)
131 #ifdef IMAGE_DEBUG_FLAG
132 #define IMAGE_INFO(fmt, ...) DECORATOR_HILOG(HILOG_INFO, fmt, ##__VA_ARGS__)
133 #define IMAGE_DEBUG(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt, ##__VA_ARGS__)
134 #define IMAGE_FUNCTION_IN(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}s IN", ##__VA_ARGS__, __FUNCTION__)
135 #define IMAGE_FUNCTION_OUT(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}s OUT", ##__VA_ARGS__, __FUNCTION__)
136 #define IMAGE_LINE_IN(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}d IN", ##__VA_ARGS__, __LINE__)
137 #define IMAGE_LINE_OUT(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}d OUT", ##__VA_ARGS__, __LINE__)
138 #else
139 #define IMAGE_INFO(fmt, ...)
140 #define IMAGE_DEBUG(fmt, ...)
141 #define IMAGE_FUNCTION_IN(fmt, ...)
142 #define IMAGE_FUNCTION_OUT(fmt, ...)
143 #define IMAGE_LINE_IN(fmt, ...)
144 #define IMAGE_LINE_OUT(fmt, ...)
145 #endif
146 
147 namespace OHOS {
148 namespace Media {
149 class ImageNapiUtils {
150 public:
151     static bool GetBufferByName(napi_env env, napi_value root, const char* name, void **res, size_t* len);
152     static bool GetUint32ByName(napi_env env, napi_value root, const char* name, uint32_t *res);
153     static bool GetInt32ByName(napi_env env, napi_value root, const char* name, int32_t *res);
154     static bool GetDoubleByName(napi_env env, napi_value root, const char* name, double *res);
155     static bool GetBoolByName(napi_env env, napi_value root, const char* name, bool *res);
156     static bool GetNodeByName(napi_env env, napi_value root, const char* name, napi_value *res);
157     static bool GetUtf8String(napi_env env, napi_value root, std::string &res, bool eof = true);
158     static napi_valuetype getType(napi_env env, napi_value root);
159     static bool CreateArrayBuffer(napi_env env, void* src, size_t srcLen, napi_value *res);
160     static bool CreateNapiInt32(napi_env env, int32_t value, napi_value &root);
161     static bool CreateNapiDouble(napi_env env, double value, napi_value &root);
162     static bool ParseImageCreatorReceiverArgs(napi_env env, size_t argc,
163         napi_value argv[], int32_t args[], std::string &errMsg);
164     static void HicheckerReport();
165     static void CreateErrorObj(napi_env env, napi_value &errorObj,
166         const int32_t errCode, const std::string errMsg);
167     static napi_value ThrowExceptionError(napi_env env, const int32_t errCode, const std::string errMsg);
168 };
169 } // namespace Media
170 } // namespace OHOS
171 #endif // FRAMEWORKS_KITS_JS_COMMON_INCLUDE_IMAGE_NAPI_UTILS_H
172