diff --git a/interfaces/kits/napi/graphic/ui_effect/BUILD.gn b/interfaces/kits/napi/graphic/ui_effect/BUILD.gn index 83ac45534a..c7dcfb1f9d 100644 --- a/interfaces/kits/napi/graphic/ui_effect/BUILD.gn +++ b/interfaces/kits/napi/graphic/ui_effect/BUILD.gn @@ -58,8 +58,6 @@ ohos_shared_library("uieffect_napi") { external_deps = [ "hilog:libhilog", - "image_framework:image", - "image_framework:image_native", "napi:ace_napi", ] diff --git a/interfaces/kits/napi/graphic/ui_effect/include/ui_effect_napi_utils.h b/interfaces/kits/napi/graphic/ui_effect/include/ui_effect_napi_utils.h new file mode 100644 index 0000000000..f323c9e32d --- /dev/null +++ b/interfaces/kits/napi/graphic/ui_effect/include/ui_effect_napi_utils.h @@ -0,0 +1,57 @@ +/* + * 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_UIEFFECT_NAPI_UTILS_H +#define OHOS_UIEFFECT_NAPI_UTILS_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +#define UIEFFECT_IS_OK(x) ((x) == napi_ok) +#define UIEFFECT_NOT_NULL(p) ((p) != nullptr) +#define UIEFFECT_IS_READY(x, p) (UIEFFECT_IS_OK(x) && UIEFFECT_NOT_NULL(p)) + +#define UIEFFECT_NAPI_CHECK_RET_D(x, res, msg) \ +do \ +{ \ + if (!(x)) \ + { \ + msg; \ + return (res); \ + } \ +} while (0) + +#define UIEFFECT_JS_ARGS(env, info, status, argc, argv, thisVar) \ +do \ +{ \ + status = napi_get_cb_info(env, info, &(argc), argv, &(thisVar), nullptr); \ +} while (0) + +#define UIEFFECT_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) + +namespace OHOS { +namespace Rosen { +class UIEffectNapiUtils { +public: + static napi_valuetype getType(napi_env env, napi_value root) + { + napi_valuetype res = napi_undefined; + napi_typeof(env, root, &res); + return res; + } +}; +} // namespace Rosen +} // namespace OHOS +#endif // OHOS_UIEFFECT_NAPI_UTILS_H \ No newline at end of file diff --git a/interfaces/kits/napi/graphic/ui_effect/src/effect_napi.cpp b/interfaces/kits/napi/graphic/ui_effect/src/effect_napi.cpp index 296ca64dfb..a3cba041b1 100644 --- a/interfaces/kits/napi/graphic/ui_effect/src/effect_napi.cpp +++ b/interfaces/kits/napi/graphic/ui_effect/src/effect_napi.cpp @@ -13,21 +13,14 @@ * limitations under the License. */ #include "effect_napi.h" -#include "image_napi_utils.h" - +#include "ui_effect_napi_utils.h" + namespace { - constexpr uint32_t NUM_0 = 0; constexpr uint32_t NUM_1 = 1; - constexpr uint32_t NUM_2 = 2; constexpr uint32_t NUM_3 = 3; - constexpr uint32_t NUM_4 = 4; - constexpr uint32_t NUM_5 = 5; - constexpr uint32_t NUM_6 = 6; - constexpr uint32_t NUM_7 = 7; constexpr uint32_t NUM_8 = 8; - constexpr uint32_t NUM_9 = 9; } - + namespace OHOS { namespace Rosen { static const std::string CLASS_NAME = "VisualEffect"; @@ -56,29 +49,28 @@ napi_value EffectNapi::Init(napi_env env, napi_value exports) nullptr, sizeof(static_prop) / sizeof(static_prop[0]), static_prop, &constructor); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, UIEFFECT_LOG_E("define class fail")); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, UIEFFECT_LOG_E("define class fail")); status = napi_create_reference(env, constructor, 1, &sConstructor_); - if (!IMG_IS_OK(status)) { + if (!UIEFFECT_IS_OK(status)) { UIEFFECT_LOG_I("EffectNapi Init napi_create_reference falid"); return nullptr; } napi_value global = nullptr; status = napi_get_global(env, &global); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, UIEFFECT_LOG_E("Init:get global fail")); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, UIEFFECT_LOG_E("Init:get global fail")); status = napi_set_named_property(env, global, CLASS_NAME.c_str(), constructor); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, UIEFFECT_LOG_E("Init:set global named property fail")); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, UIEFFECT_LOG_E("Init:set global named property fail")); status = napi_set_named_property(env, exports, CLASS_NAME.c_str(), constructor); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, UIEFFECT_LOG_E("set named property fail")); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, UIEFFECT_LOG_E("set named property fail")); - status = napi_define_properties(env, exports, IMG_ARRAY_SIZE(static_prop), static_prop); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, UIEFFECT_LOG_E("define properties fail")); + status = napi_define_properties(env, exports, UIEFFECT_ARRAY_SIZE(static_prop), static_prop); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, UIEFFECT_LOG_E("define properties fail")); return exports; } - napi_value EffectNapi::Constructor(napi_env env, napi_callback_info info) { size_t argCount = 0; @@ -107,7 +99,7 @@ void EffectNapi::Destructor(napi_env env, void* nativeObject, void* finalize) { EffectNapi *effectNapi = reinterpret_cast(nativeObject); - if (IMG_NOT_NULL(effectNapi)) { + if (UIEFFECT_NOT_NULL(effectNapi)) { effectNapi->~EffectNapi(); } } @@ -134,10 +126,59 @@ napi_value EffectNapi::CreateEffect(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_define_properties(env, object, sizeof(resultFuncs) / sizeof(resultFuncs[0]), resultFuncs)); return object; } - + +napi_value ParseJsValue(napi_env env, napi_value jsObject, const std::string& name) +{ + napi_value value = nullptr; + napi_get_named_property(env, jsObject, name.c_str(), &value); + return value; +} + +bool CheckCreateBrightnessBlender(napi_env env, napi_value jsObject) +{ + bool result = true; + napi_status status = napi_has_named_property(env, jsObject, "cubicRate", &result); + if (!((status == napi_ok) && result)) { + return false; + } + status = napi_has_named_property(env, jsObject, "quadraticRate", &result); + if (!((status == napi_ok) && result)) { + return false; + } + status = napi_has_named_property(env, jsObject, "linearRate", &result); + if (!((status == napi_ok) && result)) { + return false; + } + status = napi_has_named_property(env, jsObject, "degree", &result); + if (!((status == napi_ok) && result)) { + return false; + } + status = napi_has_named_property(env, jsObject, "saturation", &result); + if (!((status == napi_ok) && result)) { + return false; + } + status = napi_has_named_property(env, jsObject, "fraction", &result); + if (!((status == napi_ok) && result)) { + return false; + } + status = napi_has_named_property(env, jsObject, "positiveCoefficient", &result); + if (!((status == napi_ok) && result)) { + return false; + } + status = napi_has_named_property(env, jsObject, "negativeCoefficient", &result); + if (!((status == napi_ok) && result)) { + return false; + } + return true; +} + napi_value EffectNapi::CreateBrightnessBlender(napi_env env, napi_callback_info info) { BrightnessBlender* blender = new(std::nothrow) BrightnessBlender(); + if (blender == nullptr) { + UIEFFECT_LOG_E("CreateBrightnessBlender blender is nullptr"); + return nullptr; + } napi_value object = nullptr; napi_create_object(env, &object); napi_wrap( @@ -147,26 +188,36 @@ napi_value EffectNapi::CreateBrightnessBlender(napi_env env, napi_callback_info delete blenderObj; }, nullptr, nullptr); - - napi_value argValue[NUM_9] = {0}; - size_t argCount = NUM_9; - napi_status status; - IMG_JS_ARGS(env, info, status, argCount, argValue, object); - - if (argCount < NUM_8) { - UIEFFECT_LOG_E("EffectNapi CreateBrightnessBlender object is Faild"); + + size_t argc = 1; + napi_value argv[1]; + napi_value thisVar = nullptr; + napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); + if (argc != 1) { + UIEFFECT_LOG_E("EffectNapi SetbackgroundColorBlender input check failed, argc number is not 1."); return nullptr; } - - napi_set_named_property(env, object, "cubicRate", argValue[NUM_0]); - napi_set_named_property(env, object, "quadRate", argValue[NUM_1]); - napi_set_named_property(env, object, "linearRate", argValue[NUM_2]); - napi_set_named_property(env, object, "degree", argValue[NUM_3]); - napi_set_named_property(env, object, "saturation", argValue[NUM_4]); - napi_set_named_property(env, object, "positiveCoeff", argValue[NUM_5]); - napi_set_named_property(env, object, "negativeCoeff", argValue[NUM_6]); - napi_set_named_property(env, object, "fraction", argValue[NUM_7]); - + + napi_value nativeObj = argv[0]; + if (nativeObj == nullptr) { + UIEFFECT_LOG_E("EffectNapi SetbackgroundColorBlender input check failed, nativeObj is nullptr."); + return nullptr; + } + + if (!CheckCreateBrightnessBlender (env, nativeObj)) { + UIEFFECT_LOG_E("EffectNapi CheckCreateBrightnessBlender failed."); + return nullptr; + } + + napi_set_named_property(env, object, "cubicRate", ParseJsValue(env, nativeObj, "cubicRate")); + napi_set_named_property(env, object, "quadraticRate", ParseJsValue(env, nativeObj, "quadraticRate")); + napi_set_named_property(env, object, "linearRate", ParseJsValue(env, nativeObj, "linearRate")); + napi_set_named_property(env, object, "degree", ParseJsValue(env, nativeObj, "degree")); + napi_set_named_property(env, object, "saturation", ParseJsValue(env, nativeObj, "saturation")); + napi_set_named_property(env, object, "positiveCoefficient", ParseJsValue(env, nativeObj, "positiveCoefficient")); + napi_set_named_property(env, object, "negativeCoefficient", ParseJsValue(env, nativeObj, "negativeCoefficient")); + napi_set_named_property(env, object, "fraction", ParseJsValue(env, nativeObj, "fraction")); + if (object == nullptr) { UIEFFECT_LOG_E("EffectNapi CreateBrightnessBlender object is Faild"); } @@ -210,7 +261,7 @@ bool ParseJsVec3Value(napi_value jsObject, napi_env env, const std::string& name napi_get_named_property(env, jsObject, name.c_str(), ¶m); napi_valuetype valueType = napi_undefined; - valueType = Media::ImageNapiUtils::getType(env, param); + valueType = UIEffectNapiUtils::getType(env, param); if (valueType == napi_undefined) { return true; } @@ -250,7 +301,7 @@ bool EffectNapi::ParseBrightnessBlender( blender->SetCubicRate(static_cast(val)); parseTimes++; } - if (ParseJsDoubleValue(jsObject, env, "quadRate", val)) { + if (ParseJsDoubleValue(jsObject, env, "quadraticRate", val)) { blender->SetQuadRate(static_cast(val)); parseTimes++; } @@ -270,11 +321,11 @@ bool EffectNapi::ParseBrightnessBlender( blender->SetFraction(static_cast(val)); parseTimes++; } - if (ParseJsVec3Value(jsObject, env, "positiveCoeff", tmpVector3)) { + if (ParseJsVec3Value(jsObject, env, "positiveCoefficient", tmpVector3)) { blender->SetPositiveCoeff(tmpVector3); parseTimes++; } - if (ParseJsVec3Value(jsObject, env, "negativeCoeff", tmpVector3)) { + if (ParseJsVec3Value(jsObject, env, "negativeCoefficient", tmpVector3)) { blender->SetNegativeCoeff(tmpVector3); parseTimes++; } @@ -287,7 +338,7 @@ napi_value EffectNapi::SetbackgroundColorBlender(napi_env env, napi_callback_inf napi_value thisVar = nullptr; napi_value argValue[NUM_1] = {0}; size_t argCount = NUM_1; - IMG_JS_ARGS(env, info, status, argCount, argValue, thisVar); + UIEFFECT_JS_ARGS(env, info, status, argCount, argValue, thisVar); if (status != napi_ok) { UIEFFECT_LOG_E("EffectNapi SetbackgroundColorBlender parsr input Faild"); diff --git a/interfaces/kits/napi/graphic/ui_effect/src/filter_napi.cpp b/interfaces/kits/napi/graphic/ui_effect/src/filter_napi.cpp index 0584104e8b..9e138c97ab 100644 --- a/interfaces/kits/napi/graphic/ui_effect/src/filter_napi.cpp +++ b/interfaces/kits/napi/graphic/ui_effect/src/filter_napi.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ #include "filter_napi.h" -#include "image_napi_utils.h" +#include "ui_effect_napi_utils.h" namespace { constexpr uint32_t NUM_0 = 0; @@ -79,25 +79,25 @@ napi_value FilterNapi::Init(napi_env env, napi_value exports) nullptr, sizeof(static_prop) / sizeof(static_prop[0]), static_prop, &constructor); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, FILTER_LOG_E("define class fail")); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, FILTER_LOG_E("define class fail")); status = napi_create_reference(env, constructor, 1, &sConstructor_); - if (!IMG_IS_OK(status)) { + if (!UIEFFECT_IS_OK(status)) { FILTER_LOG_I("FilterNapi Init napi_create_reference falid"); return nullptr; } napi_value global = nullptr; status = napi_get_global(env, &global); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, FILTER_LOG_E("Init:get global fail")); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, FILTER_LOG_E("Init:get global fail")); status = napi_set_named_property(env, global, CLASS_NAME.c_str(), constructor); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, FILTER_LOG_E("Init:set global named property fail")); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, FILTER_LOG_E("Init:set global named property fail")); status = napi_set_named_property(env, exports, CLASS_NAME.c_str(), constructor); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, FILTER_LOG_E("set named property fail")); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, FILTER_LOG_E("set named property fail")); - status = napi_define_properties(env, exports, IMG_ARRAY_SIZE(static_prop), static_prop); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, FILTER_LOG_E("define properties fail")); + status = napi_define_properties(env, exports, UIEFFECT_ARRAY_SIZE(static_prop), static_prop); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, FILTER_LOG_E("define properties fail")); auto tileModeFormat = TileModeInit(env); napi_set_named_property(env, exports, "TileMode", tileModeFormat); @@ -132,7 +132,7 @@ void FilterNapi::Destructor(napi_env env, void* nativeObject, void* finalize) { FilterNapi *filterNapi = reinterpret_cast(nativeObject); - if (IMG_NOT_NULL(filterNapi)) { + if (UIEFFECT_NOT_NULL(filterNapi)) { filterNapi->~FilterNapi(); } } @@ -168,8 +168,8 @@ napi_value FilterNapi::SetBlur(napi_env env, napi_callback_info info) napi_value argv[1]; napi_value _this; napi_status status; - IMG_JS_ARGS(env, info, status, argc, argv, _this); - if (!IMG_IS_OK(status)) { + UIEFFECT_JS_ARGS(env, info, status, argc, argv, _this); + if (!UIEFFECT_IS_OK(status)) { FILTER_LOG_I("FilterNapi parse input falid"); return _this; } @@ -177,9 +177,9 @@ napi_value FilterNapi::SetBlur(napi_env env, napi_callback_info info) if (argc != 1) { return _this; } - if (Media::ImageNapiUtils::getType(env, argv[0]) == napi_number) { + if (UIEffectNapiUtils::getType(env, argv[0]) == napi_number) { double tmp = 0.0f; - if (IMG_IS_OK(napi_get_value_double(env, argv[0], &tmp))) { + if (UIEFFECT_IS_OK(napi_get_value_double(env, argv[0], &tmp))) { if (tmp >= 0) { radius = static_cast(tmp); } @@ -218,7 +218,7 @@ static bool IsArrayForNapiValue(napi_env env, napi_value param, uint32_t &arrayS static bool GetStretchPercent(napi_env env, napi_value param, std::shared_ptr& para) { napi_valuetype valueType = napi_undefined; - valueType = Media::ImageNapiUtils::getType(env, param); + valueType = UIEffectNapiUtils::getType(env, param); if (valueType == napi_undefined) { return true; } @@ -258,14 +258,14 @@ napi_value FilterNapi::SetPixelStretch(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; napi_value argValue[NUM_3] = {0}; size_t argCount = NUM_3; - IMG_JS_ARGS(env, info, status, argCount, argValue, thisVar); - IMG_NAPI_CHECK_RET_D(IMG_IS_OK(status), nullptr, FILTER_LOG_E("fail to napi_get_cb_info")); + UIEFFECT_JS_ARGS(env, info, status, argCount, argValue, thisVar); + UIEFFECT_NAPI_CHECK_RET_D(UIEFFECT_IS_OK(status), nullptr, FILTER_LOG_E("fail to napi_get_cb_info")); Drawing::TileMode tileMode = Drawing::TileMode::CLAMP; std::shared_ptr para = std::make_shared(); if (argCount >= NUM_1) { - IMG_NAPI_CHECK_RET_D(GetStretchPercent(env, argValue[NUM_0], para), + UIEFFECT_NAPI_CHECK_RET_D(GetStretchPercent(env, argValue[NUM_0], para), nullptr, FILTER_LOG_E("fail to parse coordinates")); } if (argCount >= NUM_2) { @@ -286,9 +286,9 @@ napi_value FilterNapi::SetPixelStretch(napi_env env, napi_callback_info info) Drawing::TileMode FilterNapi::ParserArgumentType(napi_env env, napi_value argv) { int32_t mode = 0; - if (Media::ImageNapiUtils::getType(env, argv) == napi_number) { + if (UIEffectNapiUtils::getType(env, argv) == napi_number) { double tmp = 0.0f; - if (IMG_IS_OK(napi_get_value_double(env, argv, &tmp))) { + if (UIEFFECT_IS_OK(napi_get_value_double(env, argv, &tmp))) { mode = tmp; } } diff --git a/rosen/modules/render_service_client/core/ui/rs_node.cpp b/rosen/modules/render_service_client/core/ui/rs_node.cpp index 2b08a4629a..716ab15cb4 100755 --- a/rosen/modules/render_service_client/core/ui/rs_node.cpp +++ b/rosen/modules/render_service_client/core/ui/rs_node.cpp @@ -1266,12 +1266,30 @@ void RSNode::SetOutlineRadius(const Vector4f& radius) void RSNode::SetUIBackgroundFilter(const OHOS::Rosen::Filter* backgroundFilter) { - // To do: generate composed filter here. + // To do: generate composed filter here. Now we just set background blur in v1.0. + auto filterParas = backgroundFilter->GetAllPara(); + for (const auto& filterPara : filterParas) { + if (filterPara->GetParaType() == FilterPara::BLUR) { + auto filterBlurPara = std::static_pointer_cast(filterPara); + auto blurRadius = filterBlurPara->GetRadius(); + SetBackgroundBlurRadiusX(blurRadius); + SetBackgroundBlurRadiusY(blurRadius); + } + } } void RSNode::SetUICompositingFilter(const OHOS::Rosen::Filter* compositingFilter) { - // To do: generate composed filter here. + // To do: generate composed filter here. Now we just set compositing blur in v1.0. + auto filterParas = compositingFilter->GetAllPara(); + for (const auto& filterPara : filterParas) { + if (filterPara->GetParaType() == FilterPara::BLUR) { + auto filterBlurPara = std::static_pointer_cast(filterPara); + auto blurRadius = filterBlurPara->GetRadius(); + SetForegroundBlurRadiusX(blurRadius); + SetForegroundBlurRadiusY(blurRadius); + } + } } void RSNode::SetUIForegroundFilter(const OHOS::Rosen::Filter* foregroundFilter) diff --git a/rosen/modules/render_service_client/core/ui/rs_node.h b/rosen/modules/render_service_client/core/ui/rs_node.h index 1fe6c0bdcc..4b755110d4 100755 --- a/rosen/modules/render_service_client/core/ui/rs_node.h +++ b/rosen/modules/render_service_client/core/ui/rs_node.h @@ -38,6 +38,7 @@ #include "ui_effect/effect/include/visual_effect.h" #include "ui_effect/filter/include/filter.h" #include "ui_effect/filter/include/filter_pixel_stretch_para.h" +#include "ui_effect/filter/include/filter_blur_para.h" #include "recording/recording_canvas.h" diff --git a/rosen/modules/render_service_client/core/ui_effect/effect/include/brightness_blender.h b/rosen/modules/render_service_client/core/ui_effect/effect/include/brightness_blender.h index b79e23d890..dec832ecf1 100644 --- a/rosen/modules/render_service_client/core/ui_effect/effect/include/brightness_blender.h +++ b/rosen/modules/render_service_client/core/ui_effect/effect/include/brightness_blender.h @@ -109,14 +109,14 @@ public: } private: - float cubicRate_; - float quadRate_; - float linearRate_; - float degree_; + float cubicRate_ = 0.0f; + float quadRate_ = 0.0f; + float linearRate_ = 1.0f; + float degree_ = 0.0f; float saturation_ = 1.0f; Vector3f positiveCoeff_; Vector3f negativeCoeff_; - float fraction_; + float fraction_ = 1.0f; }; } // namespace Rosen } // namespace OHOS