From 00a03c33af5e4977fad32cab7f9048ad29a4021b Mon Sep 17 00:00:00 2001 From: yihuiyang Date: Thu, 23 Nov 2023 19:46:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BB=8Eenv=E8=8E=B7=E5=8F=96=E5=A4=9A?= =?UTF-8?q?=E4=B8=AAXComponent=E8=BF=9B=E8=A1=8C=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yihuiyang --- .../platform/ohos/ohos_xcomponent_adapter.cpp | 131 ++++++++++-------- shell/platform/ohos/ohos_xcomponent_adapter.h | 48 ++++--- 2 files changed, 103 insertions(+), 76 deletions(-) diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.cpp b/shell/platform/ohos/ohos_xcomponent_adapter.cpp index 21fa8c51fd..ed6e88a6b2 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -16,31 +16,10 @@ #include "ohos_xcomponent_adapter.h" #include "flutter/shell/platform/ohos/napi/platform_view_ohos_napi.h" #include "types.h" +#include namespace flutter { -enum ContextType { - APP_LIFECYCLE = 0, - JS_PAGE_LIFECYCLE, -}; - XComponentAdapter XComponentAdapter::mXComponentAdapter; -OH_NativeXComponent_Callback XComponentAdapter::callback_; - -void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) { - XComponentAdapter::GetInstance()->OnSurfaceCreated(component, window); -} - -void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window) { - XComponentAdapter::GetInstance()->OnSurfaceChanged(component, window); -} - -void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) { - XComponentAdapter::GetInstance()->OnSurfaceDestroyed(component, window); -} - -void DispatchTouchEventCB(OH_NativeXComponent* component, void* window) { - XComponentAdapter::GetInstance()->DispatchTouchEvent(component, window); -} XComponentAdapter::XComponentAdapter(/* args */) {} @@ -80,7 +59,6 @@ bool XComponentAdapter::Export(napi_env env, napi_value exports) { std::string id(idStr); auto context = XComponentAdapter::GetInstance(); if (context) { - XComponentAdapter::shell_holder = PlatformViewOHOSNapi::GetShellHolder(); context->SetNativeXComponent(id, nativeXComponent); return true; } @@ -91,14 +69,11 @@ bool XComponentAdapter::Export(napi_env env, napi_value exports) { void XComponentAdapter::SetNativeXComponent( std::string& id, OH_NativeXComponent* nativeXComponent) { - nativeXComponent_ = nativeXComponent; - BindXComponentCallback(); - OH_NativeXComponent_RegisterCallback(nativeXComponent_, - &XComponentAdapter::callback_); -} - -OH_NativeXComponent* XComponentAdapter::GetNativeXComponent(std::string& id) { - return nativeXComponent_; + auto iter = xcomponetMap_.find(id); + if(iter == xcomponetMap_.end()) { + XComponentBase* xcomponet = new XComponentBase(id, nativeXComponent); + xcomponetMap_[id] = xcomponet; + } } #include @@ -189,8 +164,66 @@ static int32_t SetNativeWindowOpt(OHNativeWindow* nativeWindow, } return ret; } -void XComponentAdapter::OnSurfaceCreated(OH_NativeXComponent* component, - void* window) { + +void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) { + for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) + { + if(it.second->nativeXComponent_ == component) { + it.second->OnSurfaceCreated(component, window); + } + } +} + +void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window) { + for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) + { + if(it.second->nativeXComponent_ == component) { + it.second->OnSurfaceChanged(component, window); + } + } +} + +void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) { + for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) + { + if(it.second->nativeXComponent_ == component) { + it.second->OnSurfaceDestroyed(component, window); + } + } + +} +void DispatchTouchEventCB(OH_NativeXComponent* component, void* window) { + for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) + { + if(it.second->nativeXComponent_ == component) { + it.second->OnDispatchTouchEvent(component, window); + } + } +} + +void XComponentBase::BindXComponentCallback() { + callback_.OnSurfaceCreated = OnSurfaceCreatedCB; + callback_.OnSurfaceChanged = OnSurfaceChangedCB; + callback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB; + callback_.DispatchTouchEvent = DispatchTouchEventCB; +} + +XComponentBase::XComponentBase(std::string id,OH_NativeXComponent* xcomponet) { + nativeXComponent_ = xcomponet; + if (nativeXComponent_ != nullptr) { + id_ = id; + BindXComponentCallback(); + OH_NativeXComponent_RegisterCallback(nativeXComponent_, &callback_); + } +} + +XComponentBase::~XComponentBase() +{ + +} + +void XComponentBase::OnSurfaceCreated(OH_NativeXComponent* component, void* window) +{ LOGD( "XComponentManger::OnSurfaceCreated window = %{public}p component = " "%{public}p", @@ -210,11 +243,11 @@ void XComponentAdapter::OnSurfaceCreated(OH_NativeXComponent* component, if (ret) { LOGD("SetNativeWindowOpt failed:%{public}d", ret); } - PlatformViewOHOSNapi::SurfaceCreated(shell_holder, window); + PlatformViewOHOSNapi::SurfaceCreated(atoi(id_.c_str()), window); } -void XComponentAdapter::OnSurfaceChanged(OH_NativeXComponent* component, - void* window) { +void XComponentBase::OnSurfaceChanged(OH_NativeXComponent* component, void* window) +{ LOGD("XComponentManger::OnSurfaceChanged "); int32_t ret = OH_NativeXComponent_GetXComponentSize(component, window, &width_, &height_); @@ -222,35 +255,23 @@ void XComponentAdapter::OnSurfaceChanged(OH_NativeXComponent* component, LOGD("XComponent Current width:%{public}d,height:%{public}d", static_cast(width_), static_cast(height_)); } - PlatformViewOHOSNapi::SurfaceChanged(shell_holder, width_, height_); + PlatformViewOHOSNapi::SurfaceChanged(atoi(id_.c_str()), width_, height_); } -void XComponentAdapter::OnSurfaceDestroyed(OH_NativeXComponent* component, - void* window) { +void XComponentBase::OnSurfaceDestroyed(OH_NativeXComponent* component, void* window) +{ LOGD("XComponentManger::OnSurfaceDestroyed"); - PlatformViewOHOSNapi::SurfaceDestroyed(shell_holder); + PlatformViewOHOSNapi::SurfaceDestroyed(atoi(id_.c_str())); } -void XComponentAdapter::DispatchTouchEvent(OH_NativeXComponent* component, - void* window) { +void XComponentBase::OnDispatchTouchEvent(OH_NativeXComponent* component, void* window) +{ LOGD("XComponentManger::DispatchTouchEvent"); int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent_); if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { - ohosTouchProcessor_.HandleTouchEvent(shell_holder, component, &touchEvent_); + ohosTouchProcessor_.HandleTouchEvent(atoi(id_.c_str()), component, &touchEvent_); } } -OH_NativeXComponent_Callback* XComponentAdapter::GetNXComponentCallback() { - return &XComponentAdapter::callback_; -} - -void XComponentAdapter::BindXComponentCallback() { - auto renderCallback = XComponentAdapter::GetNXComponentCallback(); - renderCallback->OnSurfaceCreated = OnSurfaceCreatedCB; - renderCallback->OnSurfaceChanged = OnSurfaceChangedCB; - renderCallback->OnSurfaceDestroyed = OnSurfaceDestroyedCB; - renderCallback->DispatchTouchEvent = DispatchTouchEventCB; -} - } // namespace flutter diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.h b/shell/platform/ohos/ohos_xcomponent_adapter.h index 009d3f5e4a..210483e392 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.h +++ b/shell/platform/ohos/ohos_xcomponent_adapter.h @@ -20,42 +20,48 @@ #include "flutter/shell/platform/ohos/ohos_touch_processor.h" #include "napi/native_api.h" #include "napi_common.h" - +#include namespace flutter { +class XComponentBase +{ +private: + void BindXComponentCallback(); + +public: + XComponentBase(std::string id_, OH_NativeXComponent* xcomponet); + ~XComponentBase(); + + // Callback, called by ACE XComponent + void OnSurfaceCreated(OH_NativeXComponent* component, void* window); + void OnSurfaceChanged(OH_NativeXComponent* component, void* window); + void OnSurfaceDestroyed(OH_NativeXComponent* component, void* window); + void OnDispatchTouchEvent(OH_NativeXComponent* component, void* window); + + OH_NativeXComponent_TouchEvent touchEvent_; + OH_NativeXComponent_Callback callback_; + std::string id_; + OH_NativeXComponent* nativeXComponent_; + uint64_t width_; + uint64_t height_; + OhosTouchProcessor ohosTouchProcessor_; + +}; + class XComponentAdapter { public: XComponentAdapter(/* args */); ~XComponentAdapter(); static XComponentAdapter* GetInstance(); bool Export(napi_env env, napi_value exports); - OH_NativeXComponent* GetNativeXComponent(std::string& id); void SetNativeXComponent(std::string& id, OH_NativeXComponent* nativeXComponent); - OH_NativeXComponent_Callback* GetNXComponentCallback(); - - // Callback, called by ACE XComponent - void OnSurfaceCreated(OH_NativeXComponent* component, void* window); - void OnSurfaceChanged(OH_NativeXComponent* component, void* window); - void OnSurfaceDestroyed(OH_NativeXComponent* component, void* window); - void DispatchTouchEvent(OH_NativeXComponent* component, void* window); - - void BindXComponentCallback(); - void PackPointerDataAndDispatch( - const OH_NativeXComponent_TouchEvent touchEvent); public: - static OH_NativeXComponent_Callback callback_; - std::string id_; - uint64_t width_; - uint64_t height_; - OH_NativeXComponent_TouchEvent touchEvent_; + std::map xcomponetMap_; private: static XComponentAdapter mXComponentAdapter; - OH_NativeXComponent* nativeXComponent_; - int64_t shell_holder; - OhosTouchProcessor ohosTouchProcessor_; }; } // namespace flutter -- Gitee From e20e9e3adb41f20419658fb1a4cc89301289482c Mon Sep 17 00:00:00 2001 From: yihuiyang Date: Thu, 23 Nov 2023 19:46:00 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BB=8Eenv=E8=8E=B7=E5=8F=96=E5=A4=9A?= =?UTF-8?q?=E4=B8=AAXComponent=E8=BF=9B=E8=A1=8C=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yihuiyang --- .../platform/ohos/ohos_xcomponent_adapter.cpp | 131 ++++++++++-------- shell/platform/ohos/ohos_xcomponent_adapter.h | 48 ++++--- 2 files changed, 103 insertions(+), 76 deletions(-) diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.cpp b/shell/platform/ohos/ohos_xcomponent_adapter.cpp index 21fa8c51fd..16e70da53d 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -16,31 +16,10 @@ #include "ohos_xcomponent_adapter.h" #include "flutter/shell/platform/ohos/napi/platform_view_ohos_napi.h" #include "types.h" +#include namespace flutter { -enum ContextType { - APP_LIFECYCLE = 0, - JS_PAGE_LIFECYCLE, -}; - XComponentAdapter XComponentAdapter::mXComponentAdapter; -OH_NativeXComponent_Callback XComponentAdapter::callback_; - -void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) { - XComponentAdapter::GetInstance()->OnSurfaceCreated(component, window); -} - -void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window) { - XComponentAdapter::GetInstance()->OnSurfaceChanged(component, window); -} - -void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) { - XComponentAdapter::GetInstance()->OnSurfaceDestroyed(component, window); -} - -void DispatchTouchEventCB(OH_NativeXComponent* component, void* window) { - XComponentAdapter::GetInstance()->DispatchTouchEvent(component, window); -} XComponentAdapter::XComponentAdapter(/* args */) {} @@ -80,7 +59,6 @@ bool XComponentAdapter::Export(napi_env env, napi_value exports) { std::string id(idStr); auto context = XComponentAdapter::GetInstance(); if (context) { - XComponentAdapter::shell_holder = PlatformViewOHOSNapi::GetShellHolder(); context->SetNativeXComponent(id, nativeXComponent); return true; } @@ -91,14 +69,11 @@ bool XComponentAdapter::Export(napi_env env, napi_value exports) { void XComponentAdapter::SetNativeXComponent( std::string& id, OH_NativeXComponent* nativeXComponent) { - nativeXComponent_ = nativeXComponent; - BindXComponentCallback(); - OH_NativeXComponent_RegisterCallback(nativeXComponent_, - &XComponentAdapter::callback_); -} - -OH_NativeXComponent* XComponentAdapter::GetNativeXComponent(std::string& id) { - return nativeXComponent_; + auto iter = xcomponetMap_.find(id); + if(iter == xcomponetMap_.end()) { + XComponentBase* xcomponet = new XComponentBase(id, nativeXComponent); + xcomponetMap_[id] = xcomponet; + } } #include @@ -189,8 +164,66 @@ static int32_t SetNativeWindowOpt(OHNativeWindow* nativeWindow, } return ret; } -void XComponentAdapter::OnSurfaceCreated(OH_NativeXComponent* component, - void* window) { + +void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) { + for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) + { + if(it.second->nativeXComponent_ == component) { + it.second->OnSurfaceCreated(component, window); + } + } +} + +void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window) { + for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) + { + if(it.second->nativeXComponent_ == component) { + it.second->OnSurfaceChanged(component, window); + } + } +} + +void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) { + for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) + { + if(it.second->nativeXComponent_ == component) { + it.second->OnSurfaceDestroyed(component, window); + } + } + +} +void DispatchTouchEventCB(OH_NativeXComponent* component, void* window) { + for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) + { + if(it.second->nativeXComponent_ == component) { + it.second->OnDispatchTouchEvent(component, window); + } + } +} + +void XComponentBase::BindXComponentCallback() { + callback_.OnSurfaceCreated = OnSurfaceCreatedCB; + callback_.OnSurfaceChanged = OnSurfaceChangedCB; + callback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB; + callback_.DispatchTouchEvent = DispatchTouchEventCB; +} + +XComponentBase::XComponentBase(std::string id,OH_NativeXComponent* xcomponet) { + nativeXComponent_ = xcomponet; + if (nativeXComponent_ != nullptr) { + id_ = id; + BindXComponentCallback(); + OH_NativeXComponent_RegisterCallback(nativeXComponent_, &callback_); + } +} + +XComponentBase::~XComponentBase() +{ + +} + +void XComponentBase::OnSurfaceCreated(OH_NativeXComponent* component, void* window) +{ LOGD( "XComponentManger::OnSurfaceCreated window = %{public}p component = " "%{public}p", @@ -210,11 +243,11 @@ void XComponentAdapter::OnSurfaceCreated(OH_NativeXComponent* component, if (ret) { LOGD("SetNativeWindowOpt failed:%{public}d", ret); } - PlatformViewOHOSNapi::SurfaceCreated(shell_holder, window); + PlatformViewOHOSNapi::SurfaceCreated(std::stoll(id_), window); } -void XComponentAdapter::OnSurfaceChanged(OH_NativeXComponent* component, - void* window) { +void XComponentBase::OnSurfaceChanged(OH_NativeXComponent* component, void* window) +{ LOGD("XComponentManger::OnSurfaceChanged "); int32_t ret = OH_NativeXComponent_GetXComponentSize(component, window, &width_, &height_); @@ -222,35 +255,23 @@ void XComponentAdapter::OnSurfaceChanged(OH_NativeXComponent* component, LOGD("XComponent Current width:%{public}d,height:%{public}d", static_cast(width_), static_cast(height_)); } - PlatformViewOHOSNapi::SurfaceChanged(shell_holder, width_, height_); + PlatformViewOHOSNapi::SurfaceChanged(std::stoll(id_), width_, height_); } -void XComponentAdapter::OnSurfaceDestroyed(OH_NativeXComponent* component, - void* window) { +void XComponentBase::OnSurfaceDestroyed(OH_NativeXComponent* component, void* window) +{ LOGD("XComponentManger::OnSurfaceDestroyed"); - PlatformViewOHOSNapi::SurfaceDestroyed(shell_holder); + PlatformViewOHOSNapi::SurfaceDestroyed(std::stoll(id_)); } -void XComponentAdapter::DispatchTouchEvent(OH_NativeXComponent* component, - void* window) { +void XComponentBase::OnDispatchTouchEvent(OH_NativeXComponent* component, void* window) +{ LOGD("XComponentManger::DispatchTouchEvent"); int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent_); if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { - ohosTouchProcessor_.HandleTouchEvent(shell_holder, component, &touchEvent_); + ohosTouchProcessor_.HandleTouchEvent(std::stoll(id_), component, &touchEvent_); } } -OH_NativeXComponent_Callback* XComponentAdapter::GetNXComponentCallback() { - return &XComponentAdapter::callback_; -} - -void XComponentAdapter::BindXComponentCallback() { - auto renderCallback = XComponentAdapter::GetNXComponentCallback(); - renderCallback->OnSurfaceCreated = OnSurfaceCreatedCB; - renderCallback->OnSurfaceChanged = OnSurfaceChangedCB; - renderCallback->OnSurfaceDestroyed = OnSurfaceDestroyedCB; - renderCallback->DispatchTouchEvent = DispatchTouchEventCB; -} - } // namespace flutter diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.h b/shell/platform/ohos/ohos_xcomponent_adapter.h index 009d3f5e4a..210483e392 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.h +++ b/shell/platform/ohos/ohos_xcomponent_adapter.h @@ -20,42 +20,48 @@ #include "flutter/shell/platform/ohos/ohos_touch_processor.h" #include "napi/native_api.h" #include "napi_common.h" - +#include namespace flutter { +class XComponentBase +{ +private: + void BindXComponentCallback(); + +public: + XComponentBase(std::string id_, OH_NativeXComponent* xcomponet); + ~XComponentBase(); + + // Callback, called by ACE XComponent + void OnSurfaceCreated(OH_NativeXComponent* component, void* window); + void OnSurfaceChanged(OH_NativeXComponent* component, void* window); + void OnSurfaceDestroyed(OH_NativeXComponent* component, void* window); + void OnDispatchTouchEvent(OH_NativeXComponent* component, void* window); + + OH_NativeXComponent_TouchEvent touchEvent_; + OH_NativeXComponent_Callback callback_; + std::string id_; + OH_NativeXComponent* nativeXComponent_; + uint64_t width_; + uint64_t height_; + OhosTouchProcessor ohosTouchProcessor_; + +}; + class XComponentAdapter { public: XComponentAdapter(/* args */); ~XComponentAdapter(); static XComponentAdapter* GetInstance(); bool Export(napi_env env, napi_value exports); - OH_NativeXComponent* GetNativeXComponent(std::string& id); void SetNativeXComponent(std::string& id, OH_NativeXComponent* nativeXComponent); - OH_NativeXComponent_Callback* GetNXComponentCallback(); - - // Callback, called by ACE XComponent - void OnSurfaceCreated(OH_NativeXComponent* component, void* window); - void OnSurfaceChanged(OH_NativeXComponent* component, void* window); - void OnSurfaceDestroyed(OH_NativeXComponent* component, void* window); - void DispatchTouchEvent(OH_NativeXComponent* component, void* window); - - void BindXComponentCallback(); - void PackPointerDataAndDispatch( - const OH_NativeXComponent_TouchEvent touchEvent); public: - static OH_NativeXComponent_Callback callback_; - std::string id_; - uint64_t width_; - uint64_t height_; - OH_NativeXComponent_TouchEvent touchEvent_; + std::map xcomponetMap_; private: static XComponentAdapter mXComponentAdapter; - OH_NativeXComponent* nativeXComponent_; - int64_t shell_holder; - OhosTouchProcessor ohosTouchProcessor_; }; } // namespace flutter -- Gitee From e7d3d1b11afe597c1bba6345a3a668a45dee18c1 Mon Sep 17 00:00:00 2001 From: yihuiyang Date: Fri, 24 Nov 2023 15:36:52 +0800 Subject: [PATCH 3/3] =?UTF-8?q?OnSurfaceDestoryed=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E7=A7=BB=E9=99=A4XComponent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yihuiyang --- shell/platform/ohos/ohos_xcomponent_adapter.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.cpp b/shell/platform/ohos/ohos_xcomponent_adapter.cpp index 16e70da53d..29e820b67d 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -184,10 +184,15 @@ void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window) { } void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) { - for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) + for(auto it = XComponentAdapter::GetInstance()->xcomponetMap_.begin(); + it != XComponentAdapter::GetInstance()->xcomponetMap_.end();) { - if(it.second->nativeXComponent_ == component) { - it.second->OnSurfaceDestroyed(component, window); + if(it->second->nativeXComponent_ == component) { + it->second->OnSurfaceDestroyed(component, window); + delete it->second; + it = XComponentAdapter::GetInstance()->xcomponetMap_.erase(it); + } else { + ++it; } } -- Gitee