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_CREATE_CREATE_ASYNC_WORK(env, status, workName, exec, complete, aContext, work) \ 58 do \ 59 { \ 60 napi_value _resource = nullptr; \ 61 napi_create_string_utf8((env), (workName), NAPI_AUTO_LENGTH, &_resource); \ 62 (status) = napi_create_async_work(env, nullptr, _resource, (exec), \ 63 (complete), static_cast<void*>((aContext).get()), &(work)); \ 64 if ((status) == napi_ok) { \ 65 (status) = napi_queue_async_work((env), (work)); \ 66 if ((status) == napi_ok) { \ 67 (aContext).release(); \ 68 } \ 69 } \ 70 } while (0) 71 72 #define IMG_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) 73 74 #define GET_BUFFER_BY_NAME(root, name, res, len) ImageNapiUtils::GetBufferByName(env, (root), (name), &(res), &(len)) 75 #define GET_UINT32_BY_NAME(root, name, res) ImageNapiUtils::GetUint32ByName(env, (root), (name), &(res)) 76 #define GET_INT32_BY_NAME(root, name, res) ImageNapiUtils::GetInt32ByName(env, (root), (name), &(res)) 77 #define GET_BOOL_BY_NAME(root, name, res) ImageNapiUtils::GetBoolByName(env, (root), (name), &(res)) 78 #define GET_NODE_BY_NAME(root, name, res) ImageNapiUtils::GetNodeByName(env, (root), (name), &(res)) 79 80 #define STATIC_EXEC_FUNC(name) static void name ## Exec(napi_env env, void *data) 81 #define STATIC_COMPLETE_FUNC(name) static void name ## Complete(napi_env env, napi_status status, void *data) 82 83 #define DECORATOR_HILOG(op, fmt, args...) \ 84 do { \ 85 op(LABEL, fmt, ##args); \ 86 } while (0) 87 88 #define IMAGE_ERR(fmt, ...) DECORATOR_HILOG(HiLog::Error, fmt, ##__VA_ARGS__) 89 #ifdef IMAGE_DEBUG_FLAG 90 #define IMAGE_INFO(fmt, ...) DECORATOR_HILOG(HiLog::Info, fmt, ##__VA_ARGS__) 91 #define IMAGE_DEBUG(fmt, ...) DECORATOR_HILOG(HiLog::Debug, fmt, ##__VA_ARGS__) 92 #define IMAGE_FUNCTION_IN(fmt, ...) DECORATOR_HILOG(HiLog::Info, fmt "%{public}s IN", __FUNCTION__, ##__VA_ARGS__) 93 #define IMAGE_FUNCTION_OUT(fmt, ...) DECORATOR_HILOG(HiLog::Info, fmt "%{public}s OUT", __FUNCTION__, ##__VA_ARGS__) 94 #define IMAGE_LINE_IN(fmt, ...) DECORATOR_HILOG(HiLog::Info, fmt "%{public}d IN", __LINE__, ##__VA_ARGS__) 95 #define IMAGE_LINE_OUT(fmt, ...) DECORATOR_HILOG(HiLog::Info, fmt "%{public}d OUT", __LINE__, ##__VA_ARGS__) 96 #else 97 #define IMAGE_INFO(fmt, ...) 98 #define IMAGE_DEBUG(fmt, ...) 99 #define IMAGE_FUNCTION_IN(fmt, ...) 100 #define IMAGE_FUNCTION_OUT(fmt, ...) 101 #define IMAGE_LINE_IN(fmt, ...) 102 #define IMAGE_LINE_OUT(fmt, ...) 103 #endif 104 105 namespace OHOS { 106 namespace Media { 107 class ImageNapiUtils { 108 public: 109 static bool GetBufferByName(napi_env env, napi_value root, const char* name, void **res, size_t* len); 110 static bool GetUint32ByName(napi_env env, napi_value root, const char* name, uint32_t *res); 111 static bool GetInt32ByName(napi_env env, napi_value root, const char* name, int32_t *res); 112 static bool GetBoolByName(napi_env env, napi_value root, const char* name, bool *res); 113 static bool GetNodeByName(napi_env env, napi_value root, const char* name, napi_value *res); 114 static bool GetUtf8String(napi_env env, napi_value root, std::string &res, bool eof = true); 115 static napi_valuetype getType(napi_env env, napi_value root); 116 static bool CreateArrayBuffer(napi_env env, void* src, size_t srcLen, napi_value *res); 117 static void HicheckerReport(); 118 static void CreateErrorObj(napi_env env, napi_value &errorObj, 119 const int32_t errCode, const std::string errMsg); 120 static napi_value ThrowExceptionError(napi_env env, const int32_t errCode, const std::string errMsg); 121 }; 122 } // namespace Media 123 } // namespace OHOS 124 #endif // FRAMEWORKS_KITS_JS_COMMON_INCLUDE_IMAGE_NAPI_UTILS_H_ 125