diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.cpp b/shell/platform/ohos/ohos_xcomponent_adapter.cpp index a657f6b949fecfd0af46467abe5b3852a0fe8f0f..9d077520a4ab3f4e12b896ef36906c96c7f751a9 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -14,347 +14,367 @@ */ #include "ohos_xcomponent_adapter.h" + +#include + #include "flutter/shell/platform/ohos/napi/platform_view_ohos_napi.h" #include "types.h" -#include -namespace flutter { -XComponentAdapter XComponentAdapter::mXComponentAdapter; +namespace flutter { -XComponentAdapter::XComponentAdapter(/* args */) {} +XComponentAdapter::XComponentAdapter() {} XComponentAdapter::~XComponentAdapter() {} -XComponentAdapter* XComponentAdapter::GetInstance() { - return &XComponentAdapter::mXComponentAdapter; +XComponentAdapter* XComponentAdapter::GetInstance() +{ + static XComponentAdapter* instance = new XComponentAdapter; + return instance; } -bool XComponentAdapter::Export(napi_env env, napi_value exports) { - napi_status status; - napi_value exportInstance = nullptr; - OH_NativeXComponent* nativeXComponent = nullptr; - int32_t ret; - char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {}; - uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1; - - status = napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, - &exportInstance); - LOGD("napi_get_named_property,status = %{public}d", status); - if (status != napi_ok) { - return false; - } +bool XComponentAdapter::Export(napi_env env, napi_value exports) +{ + napi_status status; + napi_value export_instance = nullptr; + OH_NativeXComponent* native_xcomponent = nullptr; + int32_t ret; + char id_str[OH_XCOMPONENT_ID_LEN_MAX + 1] = {}; + uint64_t id_size = OH_XCOMPONENT_ID_LEN_MAX + 1; + + status = napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, + &export_instance); + LOGD("napi_get_named_property,status = %{public}d", status); + if (status != napi_ok) { + return false; + } - status = napi_unwrap(env, exportInstance, - reinterpret_cast(&nativeXComponent)); - LOGD("napi_unwrap,status = %{public}d", status); - if (status != napi_ok) { - return false; - } + status = napi_unwrap(env, export_instance, + reinterpret_cast(&native_xcomponent)); + LOGD("napi_unwrap,status = %{public}d", status); + if (status != napi_ok) { + return false; + } + + ret = + OH_NativeXComponent_GetXComponentId(native_xcomponent, id_str, &id_size); + LOGD("NativeXComponent id:%{public}s", id_str); + if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + return false; + } + std::string id(id_str); + auto context = XComponentAdapter::GetInstance(); + if (context) { + context->SetNativeXComponent(id, native_xcomponent); + return true; + } - ret = OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize); - LOGD("NativeXComponent id:%{public}s", idStr); - if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { return false; - } - std::string id(idStr); - auto context = XComponentAdapter::GetInstance(); - if (context) { - context->SetNativeXComponent(id, nativeXComponent); - return true; - } - - return false; } void XComponentAdapter::SetNativeXComponent( - std::string& id, - OH_NativeXComponent* nativeXComponent) { - auto iter = xcomponetMap_.find(id); - if (iter == xcomponetMap_.end()) { - XComponentBase* xcomponet = new XComponentBase(id); - xcomponetMap_[id] = xcomponet; - } - - iter = xcomponetMap_.find(id); - if (iter != xcomponetMap_.end()) { - iter->second->SetNativeXComponent(nativeXComponent); - } + const std::string& id, + OH_NativeXComponent* native_xcomponent) +{ + auto iter = xcomponent_map_.find(id); + if (iter == xcomponent_map_.end()) { + XComponentBase* xcomponet = new XComponentBase(id); + xcomponent_map_[id] = xcomponet; + } + + iter = xcomponent_map_.find(id); + if (iter != xcomponent_map_.end()) { + iter->second->SetNativeXComponent(native_xcomponent); + } } -void XComponentAdapter::AttachFlutterEngine(std::string& id, - std::string& shellholderId) { - auto iter = xcomponetMap_.find(id); - if (iter == xcomponetMap_.end()) { - XComponentBase* xcomponet = new XComponentBase(id); - xcomponetMap_[id] = xcomponet; - } - - auto findIter = xcomponetMap_.find(id); - if (findIter != xcomponetMap_.end()) { - findIter->second->AttachFlutterEngine(shellholderId); - } +void XComponentAdapter::AttachFlutterEngine( + const std::string& id, + const std::string& shell_holder_id) +{ + auto iter = xcomponent_map_.find(id); + if (iter == xcomponent_map_.end()) { + XComponentBase* xcomponet = new XComponentBase(id); + xcomponent_map_[id] = xcomponet; + } + + auto findIter = xcomponent_map_.find(id); + if (findIter != xcomponent_map_.end()) { + findIter->second->AttachFlutterEngine(shell_holder_id); + } } -void XComponentAdapter::DetachFlutterEngine(std::string& id) { - auto iter = xcomponetMap_.find(id); - if (iter != xcomponetMap_.end()) { - iter->second->DetachFlutterEngine(); - } +void XComponentAdapter::DetachFlutterEngine(const std::string& id) +{ + auto iter = xcomponent_map_.find(id); + if (iter != xcomponent_map_.end()) { + iter->second->DetachFlutterEngine(); + } } #include using OHOS_SurfaceBufferUsage = enum { - BUFFER_USAGE_CPU_READ = (1ULL << 0), /**< CPU read buffer */ - BUFFER_USAGE_CPU_WRITE = (1ULL << 1), /**< CPU write memory */ - BUFFER_USAGE_MEM_MMZ = (1ULL << 2), /**< Media memory zone (MMZ) */ - BUFFER_USAGE_MEM_DMA = (1ULL << 3), /**< Direct memory access (DMA) buffer */ - BUFFER_USAGE_MEM_SHARE = (1ULL << 4), /**< Shared memory buffer*/ - BUFFER_USAGE_MEM_MMZ_CACHE = (1ULL << 5), /**< MMZ with cache*/ - BUFFER_USAGE_MEM_FB = (1ULL << 6), /**< Framebuffer */ - BUFFER_USAGE_ASSIGN_SIZE = (1ULL << 7), /**< Memory assigned */ - BUFFER_USAGE_HW_RENDER = (1ULL << 8), /**< For GPU write case */ - BUFFER_USAGE_HW_TEXTURE = (1ULL << 9), /**< For GPU read case */ - BUFFER_USAGE_HW_COMPOSER = (1ULL << 10), /**< For hardware composer */ - BUFFER_USAGE_PROTECTED = - (1ULL << 11), /**< For safe buffer case, such as DRM */ - BUFFER_USAGE_CAMERA_READ = (1ULL << 12), /**< For camera read case */ - BUFFER_USAGE_CAMERA_WRITE = (1ULL << 13), /**< For camera write case */ - BUFFER_USAGE_VIDEO_ENCODER = (1ULL << 14), /**< For encode case */ - BUFFER_USAGE_VIDEO_DECODER = (1ULL << 15), /**< For decode case */ - BUFFER_USAGE_VENDOR_PRI0 = (1ULL << 44), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI1 = (1ULL << 45), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI2 = (1ULL << 46), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI3 = (1ULL << 47), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI4 = (1ULL << 48), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI5 = (1ULL << 49), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI6 = (1ULL << 50), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI7 = (1ULL << 51), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI8 = (1ULL << 52), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI9 = (1ULL << 53), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI10 = (1ULL << 54), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI11 = (1ULL << 55), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI12 = (1ULL << 56), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI13 = (1ULL << 57), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI14 = (1ULL << 58), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI15 = (1ULL << 59), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI16 = (1ULL << 60), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI17 = (1ULL << 61), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI18 = (1ULL << 62), /**< Reserverd for vendor */ - BUFFER_USAGE_VENDOR_PRI19 = (1ULL << 63), /**< Reserverd for vendor */ + BUFFER_USAGE_CPU_READ = (1ULL << 0), /**< CPU read buffer */ + BUFFER_USAGE_CPU_WRITE = (1ULL << 1), /**< CPU write memory */ + BUFFER_USAGE_MEM_MMZ = (1ULL << 2), /**< Media memory zone (MMZ) */ + BUFFER_USAGE_MEM_DMA = (1ULL << 3), /**< Direct memory access (DMA) buffer */ + BUFFER_USAGE_MEM_SHARE = (1ULL << 4), /**< Shared memory buffer*/ + BUFFER_USAGE_MEM_MMZ_CACHE = (1ULL << 5), /**< MMZ with cache*/ + BUFFER_USAGE_MEM_FB = (1ULL << 6), /**< Framebuffer */ + BUFFER_USAGE_ASSIGN_SIZE = (1ULL << 7), /**< Memory assigned */ + BUFFER_USAGE_HW_RENDER = (1ULL << 8), /**< For GPU write case */ + BUFFER_USAGE_HW_TEXTURE = (1ULL << 9), /**< For GPU read case */ + BUFFER_USAGE_HW_COMPOSER = (1ULL << 10), /**< For hardware composer */ + BUFFER_USAGE_PROTECTED = + (1ULL << 11), /**< For safe buffer case, such as DRM */ + BUFFER_USAGE_CAMERA_READ = (1ULL << 12), /**< For camera read case */ + BUFFER_USAGE_CAMERA_WRITE = (1ULL << 13), /**< For camera write case */ + BUFFER_USAGE_VIDEO_ENCODER = (1ULL << 14), /**< For encode case */ + BUFFER_USAGE_VIDEO_DECODER = (1ULL << 15), /**< For decode case */ + BUFFER_USAGE_VENDOR_PRI0 = (1ULL << 44), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI1 = (1ULL << 45), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI2 = (1ULL << 46), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI3 = (1ULL << 47), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI4 = (1ULL << 48), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI5 = (1ULL << 49), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI6 = (1ULL << 50), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI7 = (1ULL << 51), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI8 = (1ULL << 52), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI9 = (1ULL << 53), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI10 = (1ULL << 54), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI11 = (1ULL << 55), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI12 = (1ULL << 56), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI13 = (1ULL << 57), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI14 = (1ULL << 58), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI15 = (1ULL << 59), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI16 = (1ULL << 60), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI17 = (1ULL << 61), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI18 = (1ULL << 62), /**< Reserverd for vendor */ + BUFFER_USAGE_VENDOR_PRI19 = (1ULL << 63), /**< Reserverd for vendor */ }; -static int32_t SetNativeWindowOpt(OHNativeWindow* nativeWindow, +static int32_t SetNativeWindowOpt(OHNativeWindow* native_window, int32_t width, - int height) { - // Set the read and write scenarios of the native window buffer. - int code = SET_USAGE; - int32_t usage = - BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA; - int32_t ret = - OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, usage); - if (ret) { - LOGE( - "Set NativeWindow Usage Failed :window:%{public}p ,w:%{public}d x " - "%{public}d:%{public}d", - nativeWindow, width, height, ret); - } - // Set the width and height of the native window buffer. - code = SET_BUFFER_GEOMETRY; - ret = - OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, width, height); - if (ret) { - LOGE( - "Set NativeWindow GEOMETRY Failed :window:%{public}p ,w:%{public}d x " - "%{public}d:%{public}d", - nativeWindow, width, height, ret); - } - // Set the step of the native window buffer. - code = SET_STRIDE; - int32_t stride = 0x8; - ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, stride); - if (ret) { - LOGE( - "Set NativeWindow stride Failed :window:%{public}p ,w:%{public}d x " - "%{public}d:%{public}d", - nativeWindow, width, height, ret); - } - // Set the format of the native window buffer. - code = SET_FORMAT; - int32_t format = PIXEL_FMT_RGBA_8888; - - ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, format); - if (ret) { - LOGE( - "Set NativeWindow PIXEL_FMT_RGBA_8888 Failed :window:%{public}p " - ",w:%{public}d x %{public}d:%{public}d", - nativeWindow, width, height, ret); - } - return ret; + int height) +{ + // Set the read and write scenarios of the native window buffer. + int code = SET_USAGE; + int32_t usage = + BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA; + int32_t ret = + OH_NativeWindow_NativeWindowHandleOpt(native_window, code, usage); + if (ret) { + LOGE( + "Set NativeWindow Usage Failed :window:%{public}p ,w:%{public}d x " + "%{public}d:%{public}d", + native_window, width, height, ret); + } + // Set the width and height of the native window buffer. + code = SET_BUFFER_GEOMETRY; + ret = + OH_NativeWindow_NativeWindowHandleOpt(native_window, code, width, height); + if (ret) { + LOGE( + "Set NativeWindow GEOMETRY Failed :window:%{public}p ,w:%{public}d x " + "%{public}d:%{public}d", + native_window, width, height, ret); + } + // Set the step of the native window buffer. + code = SET_STRIDE; + int32_t stride = 0x8; + ret = OH_NativeWindow_NativeWindowHandleOpt(native_window, code, stride); + if (ret) { + LOGE( + "Set NativeWindow stride Failed :window:%{public}p ,w:%{public}d x " + "%{public}d:%{public}d", + native_window, width, height, ret); + } + // Set the format of the native window buffer. + code = SET_FORMAT; + int32_t format = PIXEL_FMT_RGBA_8888; + + ret = OH_NativeWindow_NativeWindowHandleOpt(native_window, code, format); + if (ret) { + LOGE( + "Set NativeWindow PIXEL_FMT_RGBA_8888 Failed :window:%{public}p " + ",w:%{public}d x %{public}d:%{public}d", + native_window, width, height, ret); + } + return ret; } -void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) { - for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) - { - if(it.second->nativeXComponent_ == component) { - it.second->OnSurfaceCreated(component, window); +void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window) +{ + for (auto it : XComponentAdapter::GetInstance()->xcomponent_map()) { + if (it.second->native_xcomponent() == 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 OnSurfaceChangedCB(OH_NativeXComponent* component, void* window) +{ + for (auto it : XComponentAdapter::GetInstance()->xcomponent_map()) { + if (it.second->native_xcomponent() == component) { + it.second->OnSurfaceChanged(component, window); + } } - } } -void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) { - for(auto it = XComponentAdapter::GetInstance()->xcomponetMap_.begin(); - it != XComponentAdapter::GetInstance()->xcomponetMap_.end();) - { - if(it->second->nativeXComponent_ == component) { - it->second->OnSurfaceDestroyed(component, window); - delete it->second; - it = XComponentAdapter::GetInstance()->xcomponetMap_.erase(it); - } else { - ++it; +void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) +{ + for (auto it = XComponentAdapter::GetInstance()->xcomponent_map().begin(); + it != XComponentAdapter::GetInstance()->xcomponent_map().end();) { + if (it->second->native_xcomponent() == component) { + it->second->OnSurfaceDestroyed(component, window); + delete it->second; + it = XComponentAdapter::GetInstance()->xcomponent_map().erase(it); + } else { + ++it; + } } - } - } -void DispatchTouchEventCB(OH_NativeXComponent* component, void* window) { - for(auto it: XComponentAdapter::GetInstance()->xcomponetMap_) - { - if(it.second->nativeXComponent_ == component) { - it.second->OnDispatchTouchEvent(component, window); + +void DispatchTouchEventCB(OH_NativeXComponent* component, void* window) +{ + for (auto it : XComponentAdapter::GetInstance()->xcomponent_map()) { + if (it.second->native_xcomponent() == component) { + it.second->OnDispatchTouchEvent(component, window); + } } - } } -void XComponentBase::BindXComponentCallback() { - callback_.OnSurfaceCreated = OnSurfaceCreatedCB; - callback_.OnSurfaceChanged = OnSurfaceChangedCB; - callback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB; - callback_.DispatchTouchEvent = DispatchTouchEventCB; +void XComponentBase::BindXComponentCallback() +{ + callback_.OnSurfaceCreated = OnSurfaceCreatedCB; + callback_.OnSurfaceChanged = OnSurfaceChangedCB; + callback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB; + callback_.DispatchTouchEvent = DispatchTouchEventCB; } -XComponentBase::XComponentBase(std::string id){ - id_ = id; - isEngineAttached_ = false; +XComponentBase::XComponentBase(const std::string& id) +{ + id_ = id; + isEngineAttached_ = false; } XComponentBase::~XComponentBase() {} -void XComponentBase::AttachFlutterEngine(std::string shellholderId) { - LOGD( - "XComponentManger::AttachFlutterEngine xcomponentId:%{public}s, " - "shellholderId:%{public}s", - id_.c_str(), shellholderId.c_str()); - shellholderId_ = shellholderId; - isEngineAttached_ = true; - if (window_ != nullptr) { - PlatformViewOHOSNapi::SurfaceCreated(std::stoll(shellholderId_), window_); - } else { - LOGE("OnSurfaceCreated XComponentBase is not attached"); - } +void XComponentBase::AttachFlutterEngine(const std::string& shell_holder_id) +{ + LOGD( + "XComponentManger::AttachFlutterEngine xcomponentId:%{public}s, " + "shell_holder_id:%{public}s", + id_.c_str(), shell_holder_id.c_str()); + shell_holder_id_ = shell_holder_id; + isEngineAttached_ = true; + if (window_ != nullptr) { + PlatformViewOHOSNapi::SurfaceCreated(std::stoll(shell_holder_id_), window_); + } else { + LOGE("OnSurfaceCreated XComponentBase is not attached"); + } } -void XComponentBase::DetachFlutterEngine() { - LOGD( - "XComponentManger::DetachFlutterEngine xcomponentId:%{public}s, " - "shellholderId:%{public}s", - id_.c_str(), shellholderId_.c_str()); - if (window_ != nullptr) { - PlatformViewOHOSNapi::SurfaceDestroyed(std::stoll(shellholderId_)); - } else { - LOGE("DetachFlutterEngine XComponentBase is not attached"); - } - shellholderId_ = ""; - isEngineAttached_ = false; +void XComponentBase::DetachFlutterEngine() +{ + LOGD( + "XComponentManger::DetachFlutterEngine xcomponentId:%{public}s, " + "shell_holder_id:%{public}s", + id_.c_str(), shell_holder_id_.c_str()); + if (window_ != nullptr) { + PlatformViewOHOSNapi::SurfaceDestroyed(std::stoll(shell_holder_id_)); + } else { + LOGE("DetachFlutterEngine XComponentBase is not attached"); + } + shell_holder_id_ = ""; + isEngineAttached_ = false; } -void XComponentBase::SetNativeXComponent(OH_NativeXComponent* nativeXComponent){ - nativeXComponent_ = nativeXComponent; - if (nativeXComponent_ != nullptr) { - BindXComponentCallback(); - OH_NativeXComponent_RegisterCallback(nativeXComponent_, &callback_); - } +void XComponentBase::SetNativeXComponent( + OH_NativeXComponent* native_xcomponent) +{ + native_xcomponent_ = native_xcomponent; + if (native_xcomponent_ != nullptr) { + BindXComponentCallback(); + OH_NativeXComponent_RegisterCallback(native_xcomponent_, &callback_); + } } void XComponentBase::OnSurfaceCreated(OH_NativeXComponent* component, - void* window) { - LOGD( - "XComponentManger::OnSurfaceCreated window = %{public}p component = " - "%{public}p", - window, component); - window_ = window; - int32_t ret = OH_NativeXComponent_GetXComponentSize(component, window, - &width_, &height_); - if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { - LOGD("XComponent Current width:%{public}d,height:%{public}d", - static_cast(width_), static_cast(height_)); - } else { - LOGE("GetXComponentSize result:%{public}d", ret); - } - - LOGD("OnSurfaceCreated,window.size:%{public}d,%{public}d", (int)width_, - (int)height_); - ret = SetNativeWindowOpt((OHNativeWindow*)window, width_, height_); - if (ret) { - LOGD("SetNativeWindowOpt failed:%{public}d", ret); - } - if (isEngineAttached_) { - PlatformViewOHOSNapi::SurfaceCreated(std::stoll(shellholderId_), window); - } else { - LOGE("OnSurfaceCreated XComponentBase is not attached"); - } + void* window) +{ + LOGD( + "XComponentManger::OnSurfaceCreated window = %{public}p component = " + "%{public}p", + window, component); + window_ = window; + int32_t ret = OH_NativeXComponent_GetXComponentSize(component, window, + &width_, &height_); + if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + LOGD("XComponent Current width:%{public}d,height:%{public}d", + static_cast(width_), static_cast(height_)); + } else { + LOGE("GetXComponentSize result:%{public}d", ret); + } + + LOGD("OnSurfaceCreated,window.size:%{public}d,%{public}d", (int)width_, + (int)height_); + ret = SetNativeWindowOpt((OHNativeWindow*)window, width_, height_); + if (ret) { + LOGD("SetNativeWindowOpt failed:%{public}d", ret); + } + if (isEngineAttached_) { + PlatformViewOHOSNapi::SurfaceCreated(std::stoll(shell_holder_id_), window); + } else { + LOGE("OnSurfaceCreated XComponentBase is not attached"); + } } -void XComponentBase::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_); - if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { - LOGD("XComponent Current width:%{public}d,height:%{public}d", - static_cast(width_), static_cast(height_)); - } - if (isEngineAttached_) { - PlatformViewOHOSNapi::SurfaceChanged(std::stoll(shellholderId_), width_, - height_); - } else { - LOGE("OnSurfaceChanged XComponentBase is not attached"); - } + LOGD("XComponentManger::OnSurfaceChanged "); + int32_t ret = OH_NativeXComponent_GetXComponentSize(component, window, + &width_, &height_); + if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + LOGD("XComponent Current width:%{public}d,height:%{public}d", + static_cast(width_), static_cast(height_)); + } + if (isEngineAttached_) { + PlatformViewOHOSNapi::SurfaceChanged(std::stoll(shell_holder_id_), width_, + height_); + } else { + LOGE("OnSurfaceChanged XComponentBase is not attached"); + } } void XComponentBase::OnSurfaceDestroyed(OH_NativeXComponent* component, - void* window) { - window_ = nullptr; - LOGD("XComponentManger::OnSurfaceDestroyed"); - if (isEngineAttached_) { - PlatformViewOHOSNapi::SurfaceDestroyed(std::stoll(shellholderId_)); - } else { - LOGE("OnSurfaceCreated OnSurfaceDestroyed is not attached"); - } + void* window) +{ + window_ = nullptr; + LOGD("XComponentManger::OnSurfaceDestroyed"); + if (isEngineAttached_) { + PlatformViewOHOSNapi::SurfaceDestroyed(std::stoll(shell_holder_id_)); + } else { + LOGE("OnSurfaceCreated OnSurfaceDestroyed is not attached"); + } } 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) { - if (isEngineAttached_) { - LOGD("XComponentManger::HandleTouchEvent"); - ohosTouchProcessor_.HandleTouchEvent(std::stoll(shellholderId_), - component, &touchEvent_); - } else { - LOGE( - "XComponentManger::DispatchTouchEvent XComponentBase is not " - "attached"); + void* window) +{ + LOGD("XComponentManger::DispatchTouchEvent"); + int32_t ret = + OH_NativeXComponent_GetTouchEvent(component, window, &touch_event_); + if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + if (isEngineAttached_) { + LOGD("XComponentManger::HandleTouchEvent"); + ohos_touch_processor_.HandleTouchEvent(std::stoll(shell_holder_id_), + component, &touch_event_); + } else { + LOGE( + "XComponentManger::DispatchTouchEvent XComponentBase is not " + "attached"); + } } - } } } // namespace flutter \ No newline at end of file diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.h b/shell/platform/ohos/ohos_xcomponent_adapter.h index 4fd6ff49902c6c02c1745b7bbd265a48d7f93fd0..14141e9e097d2c9d9e6379b2b5eb8f12bd13e1fd 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.h +++ b/shell/platform/ohos/ohos_xcomponent_adapter.h @@ -13,67 +13,75 @@ * limitations under the License. */ -#ifndef OHOS_XCOMPONENT_ADAPTER_H -#define OHOS_XCOMPONENT_ADAPTER_H +#ifndef PLATFORM_OHOS_OHOS_XCOMPONENT_ADAPTER_H +#define PLATFORM_OHOS_OHOS_XCOMPONENT_ADAPTER_H + #include +#include #include + #include "flutter/shell/platform/ohos/ohos_touch_processor.h" #include "napi/native_api.h" #include "napi_common.h" -#include + namespace flutter { +class XComponentBase { + public: + explicit XComponentBase(const std::string& id); + ~XComponentBase(); + + void AttachFlutterEngine(const std::string& shell_holder_id); + void DetachFlutterEngine(); + void SetNativeXComponent(OH_NativeXComponent* native_xcomponent); -class XComponentBase -{ -private: - void BindXComponentCallback(); - -public: - XComponentBase(std::string id); - ~XComponentBase(); - - void AttachFlutterEngine(std::string shellholderId); - void DetachFlutterEngine(); - void SetNativeXComponent(OH_NativeXComponent* nativeXComponent); - - // 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_; - std::string shellholderId_; - bool isEngineAttached_; - bool isWindowAttached_; - OH_NativeXComponent* nativeXComponent_; - void* window_; - uint64_t width_; - uint64_t height_; - OhosTouchProcessor ohosTouchProcessor_; + // 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* native_xcomponent() const { return native_xcomponent_; } + + private: + void BindXComponentCallback(); + + OH_NativeXComponent_TouchEvent touch_event_; + OH_NativeXComponent_Callback callback_; + std::string id_; + std::string shell_holder_id_; + bool isEngineAttached_; + OH_NativeXComponent* native_xcomponent_; + void* window_; + uint64_t width_; + uint64_t height_; + OhosTouchProcessor ohos_touch_processor_; }; +using XComponentBaseMap = std::map; class XComponentAdapter { public: - XComponentAdapter(/* args */); - ~XComponentAdapter(); - static XComponentAdapter* GetInstance(); - bool Export(napi_env env, napi_value exports); - void SetNativeXComponent(std::string& id, - OH_NativeXComponent* nativeXComponent); - void AttachFlutterEngine(std::string& id, std::string& shellholderId); - void DetachFlutterEngine(std::string& id); + static XComponentAdapter* GetInstance(); - public: - std::map xcomponetMap_; + // Forbid copy and assign + XComponentAdapter(const XComponentAdapter&) = delete; + XComponentAdapter& operator=(const XComponentAdapter&) = delete; + + bool Export(napi_env env, napi_value exports); + void SetNativeXComponent(const std::string& id, + OH_NativeXComponent* native_xcomponent); + void AttachFlutterEngine(const std::string& id, + const std::string& shell_holder_id); + void DetachFlutterEngine(const std::string& id); + + XComponentBaseMap& xcomponent_map() { return xcomponent_map_; } private: - static XComponentAdapter mXComponentAdapter; + XComponentAdapter(); + ~XComponentAdapter(); + + XComponentBaseMap xcomponent_map_; }; } // namespace flutter -#endif \ No newline at end of file +#endif // PLATFORM_OHOS_OHOS_XCOMPONENT_ADAPTER_H \ No newline at end of file