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 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 108 #define STATIC_EXEC_FUNC(name) static void name ## Exec(napi_env env, void *data) 109 #define STATIC_COMPLETE_FUNC(name) static void name ## Complete(napi_env env, napi_status status, void *data) 110 111 #define DECORATOR_HILOG(op, fmt, args...) \ 112 do { \ 113 op(LOG_CORE, fmt, ##args); \ 114 } while (0) 115 116 #define IMAGE_ERR(fmt, ...) DECORATOR_HILOG(HILOG_ERROR, fmt, ##__VA_ARGS__) 117 #ifdef IMAGE_DEBUG_FLAG 118 #define IMAGE_INFO(fmt, ...) DECORATOR_HILOG(HILOG_INFO, fmt, ##__VA_ARGS__) 119 #define IMAGE_DEBUG(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt, ##__VA_ARGS__) 120 #define IMAGE_FUNCTION_IN(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}s IN", ##__VA_ARGS__, __FUNCTION__) 121 #define IMAGE_FUNCTION_OUT(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}s OUT", ##__VA_ARGS__, __FUNCTION__) 122 #define IMAGE_LINE_IN(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}d IN", ##__VA_ARGS__, __LINE__) 123 #define IMAGE_LINE_OUT(fmt, ...) DECORATOR_HILOG(HILOG_DEBUG, fmt "%{public}d OUT", ##__VA_ARGS__, __LINE__) 124 #else 125 #define IMAGE_INFO(fmt, ...) 126 #define IMAGE_DEBUG(fmt, ...) 127 #define IMAGE_FUNCTION_IN(fmt, ...) 128 #define IMAGE_FUNCTION_OUT(fmt, ...) 129 #define IMAGE_LINE_IN(fmt, ...) 130 #define IMAGE_LINE_OUT(fmt, ...) 131 #endif 132 133 namespace OHOS { 134 namespace Media { 135 class ImageNapiUtils { 136 public: 137 static bool GetBufferByName(napi_env env, napi_value root, const char* name, void **res, size_t* len); 138 static bool GetUint32ByName(napi_env env, napi_value root, const char* name, uint32_t *res); 139 static bool GetInt32ByName(napi_env env, napi_value root, const char* name, int32_t *res); 140 static bool GetBoolByName(napi_env env, napi_value root, const char* name, bool *res); 141 static bool GetNodeByName(napi_env env, napi_value root, const char* name, napi_value *res); 142 static bool GetUtf8String(napi_env env, napi_value root, std::string &res, bool eof = true); 143 static napi_valuetype getType(napi_env env, napi_value root); 144 static bool CreateArrayBuffer(napi_env env, void* src, size_t srcLen, napi_value *res); 145 static bool ParseImageCreatorReceiverArgs(napi_env env, size_t argc, 146 napi_value argv[], int32_t args[], std::string &errMsg); 147 static void HicheckerReport(); 148 static void CreateErrorObj(napi_env env, napi_value &errorObj, 149 const int32_t errCode, const std::string errMsg); 150 static napi_value ThrowExceptionError(napi_env env, const int32_t errCode, const std::string errMsg); 151 static uint64_t GetNowTimeMicroSeconds(); 152 }; 153 } // namespace Media 154 } // namespace OHOS 155 #endif // FRAMEWORKS_KITS_JS_COMMON_INCLUDE_IMAGE_NAPI_UTILS_H 156