diff --git a/bundle.json b/bundle.json index 663faa6a1e85df135d1f08d5f65efd9b94d2e03e..79c0350b07d3bf75af1383a370502f182326dc16 100644 --- a/bundle.json +++ b/bundle.json @@ -142,6 +142,15 @@ ] }, "inner_kits": [ + { + "type": "so", + "name": "//foundation/graphic/graphic_2d/interfaces/kits/napi/graphic/common2d:common2d_napi_impl", + "header": { + "header_files": [ + ], + "header_base": "//foundation/graphic/graphic_2d/interfaces/kits/napi/graphic/common2d" + } + }, { "type": "so", "name": "//foundation/graphic/graphic_2d/interfaces/kits/napi/graphic/drawing:drawing_napi_impl", diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index 9b1801e48355bc0ac518766729af7fbd37e2f947..d9fb30c02a62355c8d9cba98d6a672d12ef3fa56 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -16,6 +16,7 @@ group("napi_packages") { "graphic/animation/window_animation_manager:windowanimationmanager_napi", "graphic/color_manager:colorspacemanager_napi", "graphic/color_manager:sendablecolorspacemanager_napi", + "graphic/common2d:common2dnapi", "graphic/drawing:drawingnapi", "graphic/effect_kit:effectkit", "graphic/hdr_capability:hdrcapability_napi", diff --git a/interfaces/kits/napi/graphic/common2d/BUILD.gn b/interfaces/kits/napi/graphic/common2d/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4855a661292f740002cd582ad3b7c14f33162cc0 --- /dev/null +++ b/interfaces/kits/napi/graphic/common2d/BUILD.gn @@ -0,0 +1,110 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +group("common2dnapi") { + deps = [ + ":common2d_napi", + ":common2d_napi_impl", + ] +} + +config("common2d_config") { + include_dirs = [ "../common2d" ] +} + +config("local_common2d_config") { + visibility = [ ":*" ] + + include_dirs = [ + "../common2d", + "../../../../../rosen/modules/2d_graphics/include", + "../../../../../rosen/modules/2d_graphics/src", + "../../../../../rosen/modules/2d_graphics/src/drawing/engine_adapter", + ] +} + +ohos_shared_library("common2d_napi") { + sources = [ + "common2d_module.cpp", + ] + + configs = [ + ":common2d_config", + ":local_common2d_config", + ] + + deps = [ ":common2d_napi_impl" ] + + external_deps = [ "napi:ace_napi" ] + + if (current_os == "ohos" || current_os == "ohos_ng") { + cflags = [ "-fstack-protector-strong" ] + cflags_cc = [ "-fstack-protector-strong" ] + } + + relative_install_dir = "module/graphics" + part_name = "graphic_2d" + subsystem_name = "graphic" +} + +ohos_shared_library("common2d_napi_impl") { + sources = [ + "utils_napi/js_utils.cpp", + "js_common2d_init.cpp", + "js_common2d_utils.cpp", + ] + + defines = [] + configs = [ ":local_common2d_config" ] + public_configs = [ ":common2d_config" ] + + deps = [ + "../../../../../interfaces/kits/napi/graphic/drawing:drawing_napi_impl", + "../../../../../rosen/modules/2d_graphics:2d_graphics", + ] + + external_deps = [ "napi:ace_napi" ] + + if (current_os == "ohos" || current_os == "ohos_ng") { + sanitize = { + boundary_sanitize = true + integer_overflow = true + ubsan = true + } + external_deps += [ + "hilog:libhilog", + "image_framework:image_native", + "init:libbegetutil", + ] + deps += [ "../../../../../utils:libgraphic_utils" ] + defines += [ "ROSEN_OHOS" ] + cflags = [ "-fstack-protector-strong" ] + cflags_cc = [ + "-fstack-protector-strong", + "-std=c++17", + ] + } else { + defines += [ "MODULE_DRAWING" ] + cflags_cc = [ "-std=c++17" ] + } + + if (current_os == "mingw") { + defines += [ "WINDOWS_PLATFORM" ] + } + + innerapi_tags = [ "platformsdk" ] + part_name = "graphic_2d" + subsystem_name = "graphic" +} diff --git a/interfaces/kits/napi/graphic/common2d/common2d_module.cpp b/interfaces/kits/napi/graphic/common2d/common2d_module.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aefe6b4088d425f6a315a06c822ee1083be3fbc1 --- /dev/null +++ b/interfaces/kits/napi/graphic/common2d/common2d_module.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "js_common2d_init.h" + +static napi_module g_commonModule = { + .nm_filename = "libcommon2d_napi_register.so/common2d.js", + .nm_register_func = OHOS::Rosen::Common2D::CommonInit, + .nm_modname = "graphics.common2D", +}; + +extern "C" __attribute__((constructor)) void NapiCommon2DAutoRegister() +{ + napi_module_register(&g_commonModule); +} \ No newline at end of file diff --git a/interfaces/kits/napi/graphic/common2d/js_common2d_init.cpp b/interfaces/kits/napi/graphic/common2d/js_common2d_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..908c2c06ba86191e5e35ea9e5abce712dd93363f --- /dev/null +++ b/interfaces/kits/napi/graphic/common2d/js_common2d_init.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_common2d_init.h" +#include "utils_napi/js_utils.h" + +namespace OHOS::Rosen { +namespace Common2D { +napi_value CommonInit(napi_env env, napi_value exportObj) +{ + JsUtils::Init(env, exportObj); + return exportObj; +} +} // namespace Common2D +} // namespace OHOS::Rosen diff --git a/interfaces/kits/napi/graphic/common2d/js_common2d_init.h b/interfaces/kits/napi/graphic/common2d/js_common2d_init.h new file mode 100644 index 0000000000000000000000000000000000000000..47bfee796076265b1d13100d8839408a7cf22550 --- /dev/null +++ b/interfaces/kits/napi/graphic/common2d/js_common2d_init.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_JS_COMMON_INIT_H +#define OHOS_JS_COMMON_INIT_H + +#include + +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" + +#include "js_common2d_utils.h" + +namespace OHOS::Rosen { +namespace Common2D { +DRAWING_API napi_value CommonInit(napi_env env, napi_value exportObj); +} // namespace Common2D +} // namespace OHOS::Rosen +#endif // OHOS_JS_COMMON_INIT_H \ No newline at end of file diff --git a/interfaces/kits/napi/graphic/common2d/js_common2d_utils.cpp b/interfaces/kits/napi/graphic/common2d/js_common2d_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38f0b4aa80c3b8e26a9087b0c7e78dea6cade482 --- /dev/null +++ b/interfaces/kits/napi/graphic/common2d/js_common2d_utils.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_common2d_utils.h" + +namespace OHOS::Rosen { +namespace Common2D { + +static const char* g_ltrbString[4] = {"left", "top", "right", "bottom"}; + +napi_value CreateJsError(napi_env env, int32_t errCode, const std::string& message) +{ + napi_value result = nullptr; + napi_create_error(env, CreateJsValue(env, errCode), CreateJsValue(env, message), &result); + return result; +} + +napi_value NapiThrowError(napi_env env, DrawingErrorCode err, const std::string& message) +{ + napi_throw(env, CreateJsError(env, static_cast(err), message)); + return nullptr; +} + +bool ConvertFromJsRect(napi_env env, napi_value jsValue, double* ltrb, size_t size) +{ + napi_value tempValue = nullptr; + for (size_t idx = 0; idx < size; idx++) { + double* curEdge = ltrb + idx; + napi_get_named_property(env, jsValue, g_ltrbString[idx], &tempValue); + if (napi_get_value_double(env, tempValue, curEdge) != napi_ok) { + return false; + } + } + return true; +} + +} // namespace Common2D +} // namespace OHOS::Rosen \ No newline at end of file diff --git a/interfaces/kits/napi/graphic/common2d/js_common2d_utils.h b/interfaces/kits/napi/graphic/common2d/js_common2d_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..919442c0627e572f9507b7c08b82027ed9d2d73c --- /dev/null +++ b/interfaces/kits/napi/graphic/common2d/js_common2d_utils.h @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_JS_COMMON2D_UTILS_H +#define OHOS_JS_COMMON2D_UTILS_H + +#include +#ifdef ROSEN_OHOS +#include "hilog/log.h" +#endif + +// #include "common/rs_common_def.h" +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" + +namespace OHOS::Rosen { + +#define CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, paramNum) \ + do { \ + size_t argc = paramNum; \ + if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok || argc != paramNum) { \ + return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, \ + std::string("Incorrect number of ") + __FUNCTION__ + " parameters."); \ + } \ + } while (0) + +namespace Common2D { + +#ifndef DRAWING_API +#ifdef _WIN32 +#define DRAWING_EXPORT __attribute__((dllexport)) +#define DRAWING_IMPORT __attribute__((dllimport)) +#else +#define DRAWING_EXPORT __attribute__((visibility("default"))) +#define DRAWING_IMPORT __attribute__((visibility("default"))) +#endif +#ifdef MODULE_DRAWING +#define DRAWING_API DRAWING_EXPORT +#else +#define DRAWING_API DRAWING_IMPORT +#endif +#endif + +constexpr size_t ARGC_ZERO = 0; +constexpr size_t ARGC_ONE = 1; +constexpr size_t ARGC_TWO = 2; +constexpr size_t ARGC_THREE = 3; +constexpr size_t ARGC_FOUR = 4; + +enum class DrawingErrorCode : int32_t { + OK = 0, + ERROR_NO_PERMISSION = 201, // the value do not change. It is defined on all system + ERROR_INVALID_PARAM = 401, // the value do not change. It is defined on all system + ERROR_DEVICE_NOT_SUPPORT = 801, // the value do not change. It is defined on all system + ERROR_ABNORMAL_PARAM_VALUE = 18600001, // the value do not change. It is defined on color manager system +}; + +inline napi_value CreateJsNumber(napi_env env, double value) +{ + napi_value result = nullptr; + napi_create_double(env, value, &result); + return result; +} + +template +napi_value CreateJsValue(napi_env env, const T& value) +{ + using ValueType = std::remove_cv_t>; + napi_value result = nullptr; + if constexpr (std::is_same_v) { + napi_get_boolean(env, value, &result); + return result; + } else if constexpr (std::is_arithmetic_v) { + return CreateJsNumber(env, value); + } else if constexpr (std::is_same_v) { + napi_create_string_utf8(env, value.c_str(), value.length(), &result); + return result; + } else if constexpr (std::is_enum_v) { + return CreateJsNumber(env, static_cast>(value)); + } else if constexpr (std::is_same_v) { + (value != nullptr) ? napi_create_string_utf8(env, value, strlen(value), &result) : + napi_get_undefined(env, &result); + return result; + } +} + +bool ConvertFromJsRect(napi_env env, napi_value jsValue, double* ltrb, size_t size); +napi_value CreateJsError(napi_env env, int32_t errCode, const std::string& message); +napi_value NapiThrowError(napi_env env, DrawingErrorCode err, const std::string& message); +} // namespace Common2D +} // namespace OHOS::Rosen + +#ifdef ROSEN_OHOS + +#undef LOG_DOMAIN +#define LOG_DOMAIN 0xD001400 + +#undef LOG_TAG +#define LOG_TAG "JsCommon2D" + +#define ROSEN_LOGI(format, ...) \ + HILOG_INFO(LOG_CORE, format, ##__VA_ARGS__) +#define ROSEN_LOGD(format, ...) \ + HILOG_DEBUG(LOG_CORE, format, ##__VA_ARGS__) +#define ROSEN_LOGE(format, ...) \ + HILOG_ERROR(LOG_CORE, format, ##__VA_ARGS__) +#else +#define ROSEN_LOGI(format, ...) +#define ROSEN_LOGD(format, ...) +#define ROSEN_LOGE(format, ...) +#endif + +#endif // OHOS_JS_COMMON2D_UTILS_H \ No newline at end of file diff --git a/interfaces/kits/napi/graphic/common2d/utils_napi/js_utils.cpp b/interfaces/kits/napi/graphic/common2d/utils_napi/js_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65d1d9bff758625f008266096da24668d0ac7c50 --- /dev/null +++ b/interfaces/kits/napi/graphic/common2d/utils_napi/js_utils.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_utils.h" + +#include "native_value.h" + +#include "js_common2d_utils.h" + +namespace OHOS::Rosen { +namespace Common2D { +thread_local napi_ref JsUtils::constructor_ = nullptr; +const std::string CLASS_NAME = "Utils"; +napi_value JsUtils::Init(napi_env env, napi_value exportObj) +{ + napi_property_descriptor properties[] = { + DECLARE_NAPI_STATIC_FUNCTION("joinRect", JsUtils::JoinRect), + }; + + napi_value constructor = nullptr; + napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr, + sizeof(properties) / sizeof(properties[0]), properties, &constructor); + if (status != napi_ok) { + ROSEN_LOGE("JsUtils::Init: Failed to define JsUtils class"); + return nullptr; + } + + status = napi_create_reference(env, constructor, 1, &constructor_); + if (status != napi_ok) { + ROSEN_LOGE("JsUtils::Init: Failed to create reference of constructor"); + return nullptr; + } + + status = napi_set_named_property(env, exportObj, CLASS_NAME.c_str(), constructor); + if (status != napi_ok) { + ROSEN_LOGE("JsUtils::Init: Failed to set constructor"); + return nullptr; + } + + return exportObj; +} + +napi_value JsUtils::Constructor(napi_env env, napi_callback_info info) +{ + size_t argCount = 0; + napi_value jsThis = nullptr; + napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr); + if (status != napi_ok) { + ROSEN_LOGE("JsUtils::Constructor: failed to napi_get_cb_info"); + return nullptr; + } + + JsUtils *jsUtils = new(std::nothrow) JsUtils(); + if (!jsUtils) { + ROSEN_LOGE("JsUtils::Constructor: Failed to create JsUtils"); + return nullptr; + } + + status = napi_wrap(env, jsThis, jsUtils, JsUtils::Destructor, nullptr, nullptr); + if (status != napi_ok) { + delete jsUtils; + ROSEN_LOGE("JsUtils::Constructor: Failed to wrap native instance"); + return nullptr; + } + return jsThis; +} + +void JsUtils::Destructor(napi_env env, void *nativeObject, void *finalize) +{ + (void)finalize; + if (nativeObject != nullptr) { + JsUtils *napi = reinterpret_cast(nativeObject); + delete napi; + } +} + +napi_value JsUtils::JoinRect(napi_env env, napi_callback_info info) +{ + napi_value argv[ARGC_TWO] = {nullptr}; + CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO); + + double ltrb[ARGC_FOUR] = {0}; + if (!ConvertFromJsRect(env, argv[ARGC_ZERO], ltrb, ARGC_FOUR)) { + return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, + "Incorrect parameter 0 type. The type of left, top, right and bottom must be number."); + } + Drawing::Rect drawingRect = Drawing::Rect(ltrb[ARGC_ZERO], ltrb[ARGC_ONE], ltrb[ARGC_TWO], ltrb[ARGC_THREE]); + + double ltrb2[ARGC_FOUR] = {0}; + if (!ConvertFromJsRect(env, argv[ARGC_ONE], ltrb2, ARGC_FOUR)) { + return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, + "Incorrect parameter 1 type. The type of left, top, right and bottom must be number."); + } + Drawing::Rect otherRect = Drawing::Rect(ltrb2[ARGC_ZERO], ltrb2[ARGC_ONE], ltrb2[ARGC_TWO], ltrb2[ARGC_THREE]); + + bool joinResult = drawingRect.Join(otherRect); + if (joinResult) { + napi_set_named_property(env, argv[ARGC_ZERO], "left", CreateJsNumber(env, drawingRect.GetLeft())); + napi_set_named_property(env, argv[ARGC_ZERO], "top", CreateJsNumber(env, drawingRect.GetTop())); + napi_set_named_property(env, argv[ARGC_ZERO], "right", CreateJsNumber(env, drawingRect.GetRight())); + napi_set_named_property(env, argv[ARGC_ZERO], "bottom", CreateJsNumber(env, drawingRect.GetBottom())); + } + + return CreateJsValue(env, joinResult); +} +} // namespace Common2D +} // namespace OHOS::Rosen \ No newline at end of file diff --git a/interfaces/kits/napi/graphic/common2d/utils_napi/js_utils.h b/interfaces/kits/napi/graphic/common2d/utils_napi/js_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..8f95d5d36d2ed6d801bfc227397460f634589693 --- /dev/null +++ b/interfaces/kits/napi/graphic/common2d/utils_napi/js_utils.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ROSEN_JS_UTILS_H +#define OHOS_ROSEN_JS_UTILS_H + +#include +#include + +#include "utils/rect.h" + +namespace OHOS::Rosen { +namespace Common2D { +class JsUtils final { +public: + JsUtils() = default; + ~JsUtils() = default; + + static napi_value Init(napi_env env, napi_value exportObj); + static napi_value Constructor(napi_env env, napi_callback_info info); + static void Destructor(napi_env env, void *nativeObject, void *finalize); + + static napi_value JoinRect(napi_env env, napi_callback_info info); + +private: + static thread_local napi_ref constructor_; +}; +} // namespace Common2D +} // namespace OHOS::Rosen +#endif // OHOS_ROSEN_JS_UTILS_H \ No newline at end of file