From 5af959616bfeffb685be7c3319afbf9bdfd97f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B0=E5=86=B0=E5=86=B0?= <418776654@qq.com> Date: Tue, 15 Apr 2025 17:36:01 +0800 Subject: [PATCH 1/2] ohos code optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 冰冰冰 <418776654@qq.com> --- tests/lua-tests/project/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lua-tests/project/CMakeLists.txt b/tests/lua-tests/project/CMakeLists.txt index c1e0df471868..6fd9a56b9495 100644 --- a/tests/lua-tests/project/CMakeLists.txt +++ b/tests/lua-tests/project/CMakeLists.txt @@ -31,7 +31,7 @@ elseif(OHOS) file(GLOB resFiles "${CMAKE_CURRENT_LIST_DIR}/../res/*") file(COPY ${resFiles} DESTINATION ${CMAKE_CURRENT_LIST_DIR}/proj.ohos/entry/src/main/resources/rawfile) file(GLOB srcFiles "${CMAKE_CURRENT_LIST_DIR}/../src/*") - file(COPY ${srcFiles} DESTINATION ${CMAKE_CURRENT_LIST_DIR}/proj.ohos/entry/src/main/resources/rawfile) + file(COPY ${srcFiles} DESTINATION ${CMAKE_CURRENT_LIST_DIR}/proj.ohos/entry/src/main/resources/rawfile/src) file(GLOB testResource ${COCOS2DX_ROOT_PATH}/tests/cpp-tests/Resources/*) file(COPY ${testResource} DESTINATION ${CMAKE_CURRENT_LIST_DIR}/proj.ohos/entry/src/main/resources/rawfile) file(GLOB cocosFiles "${COCOS2DX_ROOT_PATH}/cocos/scripting/lua-bindings/script/*") From 5bb42ce51733e6490a408b61a717f39f00d19173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B0=E5=86=B0=E5=86=B0?= <418776654@qq.com> Date: Mon, 28 Apr 2025 21:57:21 +0800 Subject: [PATCH 2/2] code optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 冰冰冰 <418776654@qq.com> --- cocos/platform/ohos/CCGLViewImpl-ohos.cpp | 83 ++----------------- cocos/platform/ohos/CCGLViewImpl-ohos.h | 2 +- .../ohos/napi/helper/Js_Cocos2dxHelper.cpp | 1 - cocos/platform/ohos/napi/helper/NapiHelper.h | 21 ++++- cocos/platform/ohos/napi/plugin_manager.cpp | 7 +- .../ohos/napi/render/plugin_render.cpp | 26 +++++- tests/cpp-tests/proj.ohos | 2 +- tests/lua-tests/project/proj.ohos | 2 +- 8 files changed, 58 insertions(+), 86 deletions(-) diff --git a/cocos/platform/ohos/CCGLViewImpl-ohos.cpp b/cocos/platform/ohos/CCGLViewImpl-ohos.cpp index edb0d8178e19..77b798f5ac95 100644 --- a/cocos/platform/ohos/CCGLViewImpl-ohos.cpp +++ b/cocos/platform/ohos/CCGLViewImpl-ohos.cpp @@ -106,82 +106,13 @@ void GLViewImpl::setIMEKeyboardState(bool bOpen) { } Rect GLViewImpl::getSafeAreaRect() const { - Rect safeAreaRect = GLView::getSafeAreaRect(); - float deviceAspectRatio = 0; - if(safeAreaRect.size.height > safeAreaRect.size.width) { - deviceAspectRatio = safeAreaRect.size.height / safeAreaRect.size.width; - } else { - deviceAspectRatio = safeAreaRect.size.width / safeAreaRect.size.height; - } - - float marginX = DEFAULT_MARGIN_OHOS / _scaleX; - float marginY = DEFAULT_MARGIN_OHOS / _scaleY; - - bool isScreenRound = JSFunction::getFunction("DeviceUtils.isRoundScreen").invoke(); - bool hasSoftKeys = JSFunction::getFunction("DeviceUtils.hasSoftKeys").invoke(); - bool isCutoutEnabled = JSFunction::getFunction("DeviceUtils.isCutoutEnable").invoke(); - - if(isScreenRound) { - // edge screen - if(safeAreaRect.size.width < safeAreaRect.size.height) { - safeAreaRect.origin.y += marginY * 2.f; - safeAreaRect.size.height -= (marginY * 2.f); - - safeAreaRect.origin.x += marginX; - safeAreaRect.size.width -= (marginX * 2.f); - } else { - safeAreaRect.origin.y += marginY; - safeAreaRect.size.height -= (marginY * 2.f); - - // landscape: no changes with X-coords - } - } else if (deviceAspectRatio >= WIDE_SCREEN_ASPECT_RATIO_OHOS) { - // almost all devices on the market have round corners - float bottomMarginIfPortrait = 0; - if(hasSoftKeys) { - bottomMarginIfPortrait = marginY * 2.f; - } - - if(safeAreaRect.size.width < safeAreaRect.size.height) { - // portrait: double margin space if device has soft menu - safeAreaRect.origin.y += bottomMarginIfPortrait; - safeAreaRect.size.height -= (bottomMarginIfPortrait + marginY); - } else { - // landscape: ignore double margin at the bottom in any cases - // prepare signle margin for round corners - safeAreaRect.origin.y += marginY; - safeAreaRect.size.height -= (marginY * 2.f); - } - } else { - if(hasSoftKeys && (safeAreaRect.size.width < safeAreaRect.size.height)) { - // portrait: preserve only for soft system menu - safeAreaRect.origin.y += marginY * 2.f; - safeAreaRect.size.height -= (marginY * 2.f); - } - } - - if (isCutoutEnabled) { - // screen with enabled cutout area - int orientation = JSFunction::getFunction("DeviceUtils.getOrientation").invoke(); - - if(static_cast(GLViewImpl::Orientation::PORTRAIT) == orientation) { - double height = JSFunction::getFunction("DeviceUtils.getCutoutHeight").invoke() / _scaleY; - safeAreaRect.origin.y += height; - safeAreaRect.size.height -= height; - } else if(static_cast(GLViewImpl::Orientation::PORTRAIT_INVERTED) == orientation) { - double height =JSFunction::getFunction("DeviceUtils.getCutoutHeight").invoke() / _scaleY; - safeAreaRect.size.height -= height; - } else if(static_cast(GLViewImpl::Orientation::LANDSCAPE) == orientation) { - double width = JSFunction::getFunction("DeviceUtils.getCutoutWidth").invoke() / _scaleX; - safeAreaRect.size.width -= width; - } else if(static_cast(GLViewImpl::Orientation::LANDSCAPE_INVERTED) == orientation) { - double width = JSFunction::getFunction("DeviceUtils.getCutoutWidth").invoke() / _scaleX; - safeAreaRect.origin.x += width; - safeAreaRect.size.width -= width; - } - } - - return safeAreaRect; + Rect safeAreaRect1; + safeAreaRect1.origin.x = JSFunction::getFunction("DeviceUtils.getSafeAreaLeft").invoke() / _scaleX; + safeAreaRect1.origin.y = JSFunction::getFunction("DeviceUtils.getSafeAreaTop").invoke() / _scaleY; + safeAreaRect1.size.width = JSFunction::getFunction("DeviceUtils.getSafeAreaWidth").invoke() / _scaleX; + safeAreaRect1.size.height = JSFunction::getFunction("DeviceUtils.getSafeAreaHeight").invoke() / _scaleX; + OHOS_LOGD("GLViewImpl getsafeAreaRect1, x:%{public}f, y:%{public}f, width:%{public}f, height:%{public}f", safeAreaRect1.origin.x, safeAreaRect1.origin.y, safeAreaRect1.size.width, safeAreaRect1.size.height); + return safeAreaRect1; } NS_CC_END diff --git a/cocos/platform/ohos/CCGLViewImpl-ohos.h b/cocos/platform/ohos/CCGLViewImpl-ohos.h index 2259d0de1796..8d760b463c2b 100644 --- a/cocos/platform/ohos/CCGLViewImpl-ohos.h +++ b/cocos/platform/ohos/CCGLViewImpl-ohos.h @@ -48,4 +48,4 @@ class CC_DLL GLViewImpl : public GLView NS_CC_END #endif // end of CC_TARGET_PLATFORM == CC_PLATFORM_OHOS -#endif // end of __CC_EGLVIEW_ANDROID_H__ +#endif // end of __CC_EGLVIEWIMPL_OHOS_H__ diff --git a/cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp b/cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp index 1576e147d1b1..b983e5ef7b11 100644 --- a/cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp +++ b/cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp @@ -15,7 +15,6 @@ napi_value Js_Cocos2dxHelper::initJsCocos2dxHelper(napi_env env, napi_callback_i * If you have more information that can be obtained asynchronously, add it here. */ napi_value Js_Cocos2dxHelper::initAsyncInfo(napi_env env, napi_callback_info info) { - JSFunction::getFunction("DeviceUtils.initScreenInfo").invoke(); return nullptr; } diff --git a/cocos/platform/ohos/napi/helper/NapiHelper.h b/cocos/platform/ohos/napi/helper/NapiHelper.h index ac662559af63..c7b48c9fe6c7 100644 --- a/cocos/platform/ohos/napi/helper/NapiHelper.h +++ b/cocos/platform/ohos/napi/helper/NapiHelper.h @@ -114,7 +114,11 @@ class JSFunction { napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...}; napi_value return_val; status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val); - + if (status == napi_pending_exception) { + LOGI("Caught invoke exception: napi_pending_exception"); + napi_value exception; + napi_get_and_clear_last_exception(env, &exception); + } ReturnType value; if (!NapiValueConverter::ToCppValue(env, return_val, value)) { // Handle error here @@ -136,6 +140,11 @@ class JSFunction { napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...}; napi_value return_val; status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val); + if (status == napi_pending_exception) { + LOGI("Caught invoke exception: napi_pending_exception"); + napi_value exception; + napi_get_and_clear_last_exception(env, &exception); + } } static void callFunctionWithParams(WorkParam *param) { @@ -167,6 +176,11 @@ class JSFunction { } if (status != napi_ok) { LOGI("XXXXXX:napi_call_function getClassObject != napi_ok %{public}d", status); + if (status == napi_pending_exception) { + LOGI("Caught invoke exception: napi_pending_exception"); + napi_value exception; + napi_get_and_clear_last_exception(env, &exception); + } } napi_value thenFunc = nullptr; @@ -187,6 +201,11 @@ class JSFunction { status = napi_call_function(env, promise, thenFunc, 1, &successFunc, &ret); if (status != napi_ok) { LOGI("XXXXXX:napi_call_function thenFunc failed, ret: %{public}d", status); + if (status == napi_pending_exception) { + LOGI("Caught invoke exception: napi_pending_exception"); + napi_value exception; + napi_get_and_clear_last_exception(env, &exception); + } } } // Callback Function Type diff --git a/cocos/platform/ohos/napi/plugin_manager.cpp b/cocos/platform/ohos/napi/plugin_manager.cpp index e23d5f51918a..4633fd855291 100644 --- a/cocos/platform/ohos/napi/plugin_manager.cpp +++ b/cocos/platform/ohos/napi/plugin_manager.cpp @@ -59,7 +59,11 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info) { int64_t value; NAPI_CALL(env, napi_get_value_int64(env, args[0], &value)); - + napi_handle_scope scope = nullptr; + NAPI_CALL(env, napi_open_handle_scope(env, &scope)); + if(scope == nullptr){ + return nullptr; + } NAPI_CALL(env, napi_create_object(env, &exports)); switch (value) { @@ -157,6 +161,7 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info) { default: OHOS_LOGE("unknown type"); } + NAPI_CALL(env, napi_close_handle_scope(env, scope)); return exports; } diff --git a/cocos/platform/ohos/napi/render/plugin_render.cpp b/cocos/platform/ohos/napi/render/plugin_render.cpp index a54a2a12cbfe..b976815df650 100644 --- a/cocos/platform/ohos/napi/render/plugin_render.cpp +++ b/cocos/platform/ohos/napi/render/plugin_render.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -31,7 +32,7 @@ float mousePositionY = -1; bool isMouseLeftActive = false; double scrollDistance = 0; -std::unordered_map ohKeyMap = { +const std::unordered_map ohKeyMap = { {KEY_ESCAPE, cocos2d::EventKeyboard::KeyCode::KEY_ESCAPE}, {KEY_GRAVE, cocos2d::EventKeyboard::KeyCode::KEY_GRAVE}, {KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS}, @@ -61,7 +62,23 @@ std::unordered_map ohKeyMap = { {KEY_DPAD_UP, cocos2d::EventKeyboard::KeyCode::KEY_DPAD_UP}, {KEY_SYSRQ, cocos2d::EventKeyboard::KeyCode::KEY_PRINT}, {KEY_INSERT, cocos2d::EventKeyboard::KeyCode::KEY_INSERT}, - {KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE} + {KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE}, + {KEY_SCROLL_LOCK, cocos2d::EventKeyboard::KeyCode::KEY_SCROLL_LOCK}, + {KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS}, + {KEY_AT, cocos2d::EventKeyboard::KeyCode::KEY_AT}, + {KEY_PLUS, cocos2d::EventKeyboard::KeyCode::KEY_PLUS}, + {KEY_MENU, cocos2d::EventKeyboard::KeyCode::KEY_MENU}, + {KEY_BREAK, cocos2d::EventKeyboard::KeyCode::KEY_PAUSE}, + {KEY_MOVE_HOME, cocos2d::EventKeyboard::KeyCode::KEY_HOME}, + {KEY_MOVE_END, cocos2d::EventKeyboard::KeyCode::KEY_END}, + {KEY_PAGE_UP, cocos2d::EventKeyboard::KeyCode::KEY_PG_UP}, + {KEY_PAGE_DOWN, cocos2d::EventKeyboard::KeyCode::KEY_PG_DOWN}, + {KEY_NUMPAD_ADD, cocos2d::EventKeyboard::KeyCode::KEY_KP_PLUS}, + {KEY_NUMPAD_SUBTRACT, cocos2d::EventKeyboard::KeyCode::KEY_KP_MINUS}, + {KEY_NUMPAD_MULTIPLY, cocos2d::EventKeyboard::KeyCode::KEY_KP_MULTIPLY}, + {KEY_NUMPAD_DIVIDE, cocos2d::EventKeyboard::KeyCode::KEY_KP_DIVIDE}, + {KEY_NUMPAD_ENTER, cocos2d::EventKeyboard::KeyCode::KEY_KP_ENTER} + }; cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyCode ohKeyCode) @@ -81,8 +98,9 @@ cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyC if (ohKeyCode >= KEY_A && ohKeyCode <= KEY_Z) { // A - Z return cocos2d::EventKeyboard::KeyCode(int(cocos2d::EventKeyboard::KeyCode::KEY_A) + (ohKeyCode - KEY_A)); - } - return cocos2d::EventKeyboard::KeyCode(ohKeyCode); + } + OHOS_LOGW("Unmapped OH key code: %d", ohKeyCode); + return cocos2d::EventKeyboard::KeyCode::KEY_NONE; } void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) diff --git a/tests/cpp-tests/proj.ohos b/tests/cpp-tests/proj.ohos index 98a643f46069..735b54750d59 160000 --- a/tests/cpp-tests/proj.ohos +++ b/tests/cpp-tests/proj.ohos @@ -1 +1 @@ -Subproject commit 98a643f46069806f94e210cd2e0386d9d112811d +Subproject commit 735b54750d593755d93bbd33988dad5121471ccb diff --git a/tests/lua-tests/project/proj.ohos b/tests/lua-tests/project/proj.ohos index 75d72fc6133b..36f664bfaab6 160000 --- a/tests/lua-tests/project/proj.ohos +++ b/tests/lua-tests/project/proj.ohos @@ -1 +1 @@ -Subproject commit 75d72fc6133ba367c0e10617042c2bf910ca93fe +Subproject commit 36f664bfaab6fb1933d1e676c92850940c14fbfb