From 663874c0be7403276533162c4985556d24f38fe8 Mon Sep 17 00:00:00 2001 From: "@wanghui187" <348582973@qq.com> Date: Mon, 24 Jun 2024 16:00:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E5=90=8E=E5=8F=B0=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E6=97=B6UV=20=E6=9A=82=E5=81=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: @wanghui187 <348582973@qq.com> --- cocos/platform/openharmony/OpenHarmonyPlatform.cpp | 12 ++++++++++-- cocos/platform/openharmony/OpenHarmonyPlatform.h | 1 + .../entry/src/main/ets/workers/cocos_worker.ts | 5 +++-- .../entry/src/main/ets/workers/cocos_worker.ts | 5 +++-- .../entry/src/main/ets/workers/cocos_worker.ts | 5 +++-- .../entry/src/main/ets/workers/cocos_worker.ts | 5 +++-- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cocos/platform/openharmony/OpenHarmonyPlatform.cpp b/cocos/platform/openharmony/OpenHarmonyPlatform.cpp index bf415c73a..fedd071f9 100644 --- a/cocos/platform/openharmony/OpenHarmonyPlatform.cpp +++ b/cocos/platform/openharmony/OpenHarmonyPlatform.cpp @@ -203,15 +203,24 @@ void OpenHarmonyPlatform::onCreateNative(napi_env env, uv_loop_t* loop) { void OpenHarmonyPlatform::onShowNative() { LOGD("OpenHarmonyPlatform::onShowNative"); EventDispatcher::dispatchOnResumeEvent(); + if(timerInited_){ + uv_timer_start(&_timerHandle, &OpenHarmonyPlatform::timerCb, 0, 1); + } } void OpenHarmonyPlatform::onHideNative() { LOGD("OpenHarmonyPlatform::onHideNative"); EventDispatcher::dispatchOnPauseEvent(); + if(timerInited_){ + uv_timer_stop(&_timerHandle); + } } void OpenHarmonyPlatform::onDestroyNative() { LOGD("OpenHarmonyPlatform::onDestroyNative"); + if(timerInited_){ + uv_timer_stop(&_timerHandle); + } } void OpenHarmonyPlatform::timerCb(uv_timer_t* handle) { @@ -241,8 +250,7 @@ void OpenHarmonyPlatform::requestVSync() { if (_workerLoop) { // Todo: Starting the timer in this way is inaccurate and will be fixed later. uv_timer_init(_workerLoop, &_timerHandle); - // The tick function needs to be called as quickly as possible because it is controlling the frame rate inside the engine. - uv_timer_start(&_timerHandle, &OpenHarmonyPlatform::timerCb, 0, 1); + timerInited_ = true; } } diff --git a/cocos/platform/openharmony/OpenHarmonyPlatform.h b/cocos/platform/openharmony/OpenHarmonyPlatform.h index 00bb3a6ac..83bd5367b 100644 --- a/cocos/platform/openharmony/OpenHarmonyPlatform.h +++ b/cocos/platform/openharmony/OpenHarmonyPlatform.h @@ -89,6 +89,7 @@ public: uv_async_t _messageSignal{}; WorkerMessageQueue _messageQueue; EGLCore* eglCore_{nullptr}; + bool timerInited_{false}; uint64_t width_; uint64_t height_; diff --git a/templates/js-template-default/frameworks/runtime-src/proj.openharmony/entry/src/main/ets/workers/cocos_worker.ts b/templates/js-template-default/frameworks/runtime-src/proj.openharmony/entry/src/main/ets/workers/cocos_worker.ts index 1a0ea84a9..3f6af8770 100644 --- a/templates/js-template-default/frameworks/runtime-src/proj.openharmony/entry/src/main/ets/workers/cocos_worker.ts +++ b/templates/js-template-default/frameworks/runtime-src/proj.openharmony/entry/src/main/ets/workers/cocos_worker.ts @@ -51,13 +51,15 @@ nativeContext.postSyncMessage = async function (msgType: string, msgData: string nativeContext.setPostMessageFunction.call(nativeContext, nativeContext.postMessage) nativeContext.setPostSyncMessageFunction.call(nativeContext, nativeContext.postSyncMessage) +const renderContext = cocos.getContext(ContextType.NATIVE_RENDER_API); +renderContext.nativeEngineStart(); + uiPort._messageHandle = function (e) { var data = e.data; var msg = data.data; switch (msg.name) { case "onXCLoad": - const renderContext = cocos.getContext(ContextType.NATIVE_RENDER_API); renderContext.nativeEngineInit(); launchEngine().then(() => { console.info('launch CC engine finished'); @@ -68,7 +70,6 @@ uiPort._messageHandle = function (e) { globalThis.oh.postMessage = nativeContext.postMessage; // @ts-ignore globalThis.oh.postSyncMessage = nativeContext.postSyncMessage; - renderContext.nativeEngineStart(); break; case "onTextInput": nativeEditBox.onTextChange(msg.param); diff --git a/templates/js-template-default/frameworks/runtime-src/proj.openharmonyv8/entry/src/main/ets/workers/cocos_worker.ts b/templates/js-template-default/frameworks/runtime-src/proj.openharmonyv8/entry/src/main/ets/workers/cocos_worker.ts index eaa709064..385849b92 100644 --- a/templates/js-template-default/frameworks/runtime-src/proj.openharmonyv8/entry/src/main/ets/workers/cocos_worker.ts +++ b/templates/js-template-default/frameworks/runtime-src/proj.openharmonyv8/entry/src/main/ets/workers/cocos_worker.ts @@ -57,19 +57,20 @@ nativeContext.postSyncMessage = async function (msgType: string, msgData: string nativeContext.setPostMessageFunction.call(nativeContext, nativeContext.postMessage) nativeContext.setPostSyncMessageFunction.call(nativeContext, nativeContext.postSyncMessage) +const renderContext = cocos.getContext(ContextType.NATIVE_RENDER_API); +renderContext.nativeEngineStart(); + uiPort._messageHandle = function (e) { var data = e.data; var msg = data.data; switch (msg.name) { case "onXCLoad": - const renderContext = cocos.getContext(ContextType.NATIVE_RENDER_API); renderContext.nativeEngineInit(); // @ts-ignore globalThis.oh.postMessage = nativeContext.postMessage; // @ts-ignore globalThis.oh.postSyncMessage = nativeContext.postSyncMessage; - renderContext.nativeEngineStart(); break; case "onTextInput": nativeEditBox.onTextChange(msg.param); diff --git a/templates/js-template-link/frameworks/runtime-src/proj.openharmony/entry/src/main/ets/workers/cocos_worker.ts b/templates/js-template-link/frameworks/runtime-src/proj.openharmony/entry/src/main/ets/workers/cocos_worker.ts index 1a0ea84a9..3f6af8770 100644 --- a/templates/js-template-link/frameworks/runtime-src/proj.openharmony/entry/src/main/ets/workers/cocos_worker.ts +++ b/templates/js-template-link/frameworks/runtime-src/proj.openharmony/entry/src/main/ets/workers/cocos_worker.ts @@ -51,13 +51,15 @@ nativeContext.postSyncMessage = async function (msgType: string, msgData: string nativeContext.setPostMessageFunction.call(nativeContext, nativeContext.postMessage) nativeContext.setPostSyncMessageFunction.call(nativeContext, nativeContext.postSyncMessage) +const renderContext = cocos.getContext(ContextType.NATIVE_RENDER_API); +renderContext.nativeEngineStart(); + uiPort._messageHandle = function (e) { var data = e.data; var msg = data.data; switch (msg.name) { case "onXCLoad": - const renderContext = cocos.getContext(ContextType.NATIVE_RENDER_API); renderContext.nativeEngineInit(); launchEngine().then(() => { console.info('launch CC engine finished'); @@ -68,7 +70,6 @@ uiPort._messageHandle = function (e) { globalThis.oh.postMessage = nativeContext.postMessage; // @ts-ignore globalThis.oh.postSyncMessage = nativeContext.postSyncMessage; - renderContext.nativeEngineStart(); break; case "onTextInput": nativeEditBox.onTextChange(msg.param); diff --git a/templates/js-template-link/frameworks/runtime-src/proj.openharmonyv8/entry/src/main/ets/workers/cocos_worker.ts b/templates/js-template-link/frameworks/runtime-src/proj.openharmonyv8/entry/src/main/ets/workers/cocos_worker.ts index eaa709064..385849b92 100644 --- a/templates/js-template-link/frameworks/runtime-src/proj.openharmonyv8/entry/src/main/ets/workers/cocos_worker.ts +++ b/templates/js-template-link/frameworks/runtime-src/proj.openharmonyv8/entry/src/main/ets/workers/cocos_worker.ts @@ -57,19 +57,20 @@ nativeContext.postSyncMessage = async function (msgType: string, msgData: string nativeContext.setPostMessageFunction.call(nativeContext, nativeContext.postMessage) nativeContext.setPostSyncMessageFunction.call(nativeContext, nativeContext.postSyncMessage) +const renderContext = cocos.getContext(ContextType.NATIVE_RENDER_API); +renderContext.nativeEngineStart(); + uiPort._messageHandle = function (e) { var data = e.data; var msg = data.data; switch (msg.name) { case "onXCLoad": - const renderContext = cocos.getContext(ContextType.NATIVE_RENDER_API); renderContext.nativeEngineInit(); // @ts-ignore globalThis.oh.postMessage = nativeContext.postMessage; // @ts-ignore globalThis.oh.postSyncMessage = nativeContext.postSyncMessage; - renderContext.nativeEngineStart(); break; case "onTextInput": nativeEditBox.onTextChange(msg.param); -- Gitee