From f474f28062ed2a55246f9ce9293c76a0e80040dc Mon Sep 17 00:00:00 2001 From: qijinquan Date: Wed, 6 Jul 2022 14:41:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9OMX=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=BA=93=EF=BC=8C=E7=9B=B4=E6=8E=A5=E5=8A=A0=E8=BD=BDOMX=5FCor?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: qijinquan --- codec/hal/BUILD.gn | 1 + codec/hal/src/codec_adapter.cpp | 10 - .../v2.0/hdi_impl/include/codec_omx_core.h | 57 ++++++ .../hal/v2.0/hdi_impl/include/component_mgr.h | 45 ++-- .../v2.0/hdi_impl/include/icomponent_mgr.h | 2 - .../hal/v2.0/hdi_impl/src/codec_omx_core.cpp | 130 ++++++++++++ codec/hal/v2.0/hdi_impl/src/component_mgr.cpp | 192 +++++------------- 7 files changed, 249 insertions(+), 188 deletions(-) create mode 100644 codec/hal/v2.0/hdi_impl/include/codec_omx_core.h create mode 100644 codec/hal/v2.0/hdi_impl/src/codec_omx_core.cpp diff --git a/codec/hal/BUILD.gn b/codec/hal/BUILD.gn index 90627f5f61..ff04150fa2 100644 --- a/codec/hal/BUILD.gn +++ b/codec/hal/BUILD.gn @@ -35,6 +35,7 @@ ohos_shared_library("libcodec_hdi_omx_server") { "src/codec_types.c", "v2.0/hdi_impl/src/codec_dyna_buffer.cpp", "v2.0/hdi_impl/src/codec_handle_buffer.cpp", + "v2.0/hdi_impl/src/codec_omx_core.cpp", "v2.0/hdi_impl/src/codec_share_buffer.cpp", "v2.0/hdi_impl/src/component_mgr.cpp", "v2.0/hdi_impl/src/component_node.cpp", diff --git a/codec/hal/src/codec_adapter.cpp b/codec/hal/src/codec_adapter.cpp index 768f9c5211..b54e3ed830 100644 --- a/codec/hal/src/codec_adapter.cpp +++ b/codec/hal/src/codec_adapter.cpp @@ -38,12 +38,6 @@ int32_t OMXAdapterCreateComponent(struct CodecComponentNode **codecNode, char *c HDF_LOGE("%{public}s create CodecComponentNode error", __func__); return HDF_ERR_MALLOC_FAIL; } - - if (!g_mgr.IsLoadLibSuc()) { - HDF_LOGE("%{public}s load lib failed", __func__); - return HDF_ERR_NOT_SUPPORT; - } - tempNode->node = std::make_shared(callbacks, appData); auto err = g_mgr.CreateComponentInstance(compName, &ComponentNode::callbacks_, tempNode->node.get(), &comp); if (err != OMX_ErrorNone) { @@ -64,10 +58,6 @@ int32_t OmxAdapterDestroyComponent(struct CodecComponentNode *codecNode) HDF_LOGE("%{public}s codecNode is null", __func__); return HDF_ERR_INVALID_PARAM; } - if (!g_mgr.IsLoadLibSuc()) { - HDF_LOGE("%{public}s load lib failed", __func__); - return HDF_ERR_NOT_SUPPORT; - } auto err = g_mgr.DeleteComponentInstance((OMX_COMPONENTTYPE *)codecNode->node->GetHandle()); if (err != OMX_ErrorNone) { HDF_LOGE("%{public}s DeleteComponentInstance err[%{public}d]", __func__, err); diff --git a/codec/hal/v2.0/hdi_impl/include/codec_omx_core.h b/codec/hal/v2.0/hdi_impl/include/codec_omx_core.h new file mode 100644 index 0000000000..659643d3f9 --- /dev/null +++ b/codec/hal/v2.0/hdi_impl/include/codec_omx_core.h @@ -0,0 +1,57 @@ +/* + * Copyright 2022 Shenzhen Kaihong DID 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 OMX_CODEC_CORE_H +#define OMX_CODEC_CORE_H +#include +#include +#include +#include +namespace OHOS { +namespace Codec { +namespace Omx { +class CodecOMXCore { +public: + typedef OMX_ERRORTYPE (*InitFunc)(); + typedef OMX_ERRORTYPE (*DeinitFunc)(); + typedef OMX_ERRORTYPE (*ComponentNameEnumFunc)(OMX_STRING, OMX_U32, OMX_U32); + typedef OMX_ERRORTYPE (*GetHandleFunc)(OMX_HANDLETYPE *, OMX_STRING, OMX_PTR, OMX_CALLBACKTYPE *); + typedef OMX_ERRORTYPE (*FreeHandleFunc)(OMX_HANDLETYPE); + typedef OMX_ERRORTYPE (*GetRolesOfComponentFunc)(OMX_STRING, OMX_U32 *, OMX_U8 **); + +public: + CodecOMXCore() = default; + ~CodecOMXCore(); + int32_t Init(const std::string &libName); + void DeInit(); + int32_t GetHandle(OMX_HANDLETYPE &handle, std::string &compName, OMX_PTR appData, + const OMX_CALLBACKTYPE &callbacks); + int32_t FreeHandle(OMX_HANDLETYPE handle); + int32_t ComponentNameEnum(std::string &name, uint32_t index); + int32_t GetRolesOfComponent(std::string &name, std::vector &roles); + +private: + void *libHandle_ = nullptr; + InitFunc init_ = nullptr; + DeinitFunc deInit_ = nullptr; + ComponentNameEnumFunc componentNameEnum_ = nullptr; + GetHandleFunc getHandle_ = nullptr; + FreeHandleFunc freeHandle_ = nullptr; + GetRolesOfComponentFunc getRoles_ = nullptr; +}; +} // namespace Omx +} // namespace Codec +} // namespace OHOS +#endif \ No newline at end of file diff --git a/codec/hal/v2.0/hdi_impl/include/component_mgr.h b/codec/hal/v2.0/hdi_impl/include/component_mgr.h index 0c587edc65..d79e9d9115 100644 --- a/codec/hal/v2.0/hdi_impl/include/component_mgr.h +++ b/codec/hal/v2.0/hdi_impl/include/component_mgr.h @@ -19,61 +19,44 @@ #include #include #include +#include #include - +#include "codec_omx_core.h" #include "icomponent_mgr.h" namespace OHOS { namespace Codec { namespace Omx { class ComponentMgr : public IComponentMgr { +public: + using OMXComponent = struct { + OMX_HANDLETYPE handle; + std::shared_ptr core; + }; + public: ComponentMgr(); ~ComponentMgr(); ComponentMgr(const ComponentMgr &) = delete; ComponentMgr &operator=(const ComponentMgr &) = delete; - virtual int32_t CreateComponentInstance(const char *componentName, const OMX_CALLBACKTYPE *callbacks, - void *appData, OMX_COMPONENTTYPE **component); + virtual int32_t CreateComponentInstance(const char *componentName, const OMX_CALLBACKTYPE *callbacks, void *appData, + OMX_COMPONENTTYPE **component); virtual int32_t DeleteComponentInstance(OMX_COMPONENTTYPE *component); - virtual int32_t EnumerateComponentsByIndex(uint32_t index, char *componentName, size_t componentNameSize); - virtual int32_t GetRolesForComponent(const char *componentName, std::vector *roles); - bool IsOMXHandleValid(OMX_COMPONENTTYPE *handle); - - bool IsLoadLibSuc() - { - return loadLibSuc_; - } - private: void AddVendorComponent(); void AddSoftComponent(); void AddComponentByLibName(const char *libName); - void AddComponentByInstance(IComponentMgr *); void CleanComponent(); private: - bool loadLibSuc_; - struct ComponentInfo { - IComponentMgr *omxComponent; - void *LibHandle; - }; - struct ComponentNameAndObjectPoint { - std::string componentName; - IComponentMgr *omxComponentMgr; - }; - struct ComponentTypePointAndObjectPoint { - OMX_COMPONENTTYPE *componentType; - IComponentMgr *omxComponentMgr; - }; - std::list componentsList_; - std::vector componentNameAndObjectPoint_; - std::vector componentTypePointAndObjectPoint_; - OMX_COMPONENTTYPE *currentComType_; - std::string currentComName_; + std::vector> cores_; // save all the core + std::map> compoentsCore_; // save the compoentname<--> core + std::vector components_; // save the opened compoents + std::mutex mutex_; }; } // namespace Omx } // namespace Codec diff --git a/codec/hal/v2.0/hdi_impl/include/icomponent_mgr.h b/codec/hal/v2.0/hdi_impl/include/icomponent_mgr.h index 970d1084c8..1e1921a533 100644 --- a/codec/hal/v2.0/hdi_impl/include/icomponent_mgr.h +++ b/codec/hal/v2.0/hdi_impl/include/icomponent_mgr.h @@ -36,8 +36,6 @@ public: virtual int32_t DeleteComponentInstance(OMX_COMPONENTTYPE *component) = 0; - virtual int32_t EnumerateComponentsByIndex(uint32_t index, char *componentName, size_t componentNameSize) = 0; - virtual int32_t GetRolesForComponent(const char *componentName, std::vector *vRoles) = 0; }; } // namespace Omx diff --git a/codec/hal/v2.0/hdi_impl/src/codec_omx_core.cpp b/codec/hal/v2.0/hdi_impl/src/codec_omx_core.cpp new file mode 100644 index 0000000000..e92c3338df --- /dev/null +++ b/codec/hal/v2.0/hdi_impl/src/codec_omx_core.cpp @@ -0,0 +1,130 @@ +/* + * Copyright 2022 Shenzhen Kaihong DID 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. + */ + +#include "codec_omx_core.h" +#include +#include +#include +#include +#include +#include +namespace OHOS { +namespace Codec { +namespace Omx { +CodecOMXCore::~CodecOMXCore() +{ + if (libHandle_ != nullptr) { + dlclose(libHandle_); + } +} +int32_t CodecOMXCore::Init(const std::string &libName) +{ + libHandle_ = dlopen(libName.c_str(), RTLD_LAZY); + if (libHandle_ == NULL) { + HDF_LOGE("%{public}s:failed to dlopen %{public}s.", __func__, libName.c_str()); + return HDF_ERR_INVALID_PARAM; + } + + init_ = (InitFunc)dlsym(libHandle_, "OMX_Init"); + deInit_ = (DeinitFunc)dlsym(libHandle_, "OMX_Deinit"); + getHandle_ = (GetHandleFunc)dlsym(libHandle_, "OMX_GetHandle"); + freeHandle_ = (FreeHandleFunc)dlsym(libHandle_, "OMX_FreeHandle"); + getRoles_ = (GetRolesOfComponentFunc)dlsym(libHandle_, "OMX_GetRolesOfComponent"); + componentNameEnum_ = (ComponentNameEnumFunc)dlsym(libHandle_, "OMX_ComponentNameEnum"); + + if (init_ != NULL) { + (*(init_))(); + } + return HDF_SUCCESS; +} + +void CodecOMXCore::DeInit() +{ + if (deInit_) { + (*deInit_)(); + } +} + +int32_t CodecOMXCore::GetHandle(OMX_HANDLETYPE &handle, std::string &compName, OMX_PTR appData, + const OMX_CALLBACKTYPE &callbacks) +{ + if (getHandle_ == nullptr) { + HDF_LOGE("%{public}s: getHandle is null.", __func__); + return HDF_ERR_INVALID_PARAM; + } + return (*getHandle_)(&handle, const_cast(compName.c_str()), appData, (OMX_CALLBACKTYPE *)&callbacks); +} + +int32_t CodecOMXCore::FreeHandle(OMX_HANDLETYPE handle) +{ + if (freeHandle_ == nullptr) { + HDF_LOGE("%{public}s: freeHandle_ is null.", __func__); + return HDF_ERR_INVALID_PARAM; + } + return (*freeHandle_)(handle); +} + +int32_t CodecOMXCore::ComponentNameEnum(std::string &name, uint32_t index) +{ + if (componentNameEnum_ == nullptr) { + HDF_LOGE("%{public}s: componentNameEnum is null.", __func__); + return HDF_ERR_INVALID_PARAM; + } + char tmpComponentName[OMX_MAX_STRINGNAME_SIZE] = {0}; + uint32_t err = (*(componentNameEnum_))(tmpComponentName, OMX_MAX_STRINGNAME_SIZE, index); + if (err == HDF_SUCCESS) { + name = tmpComponentName; + } + return err; +} +int32_t CodecOMXCore::GetRolesOfComponent(std::string &name, std::vector &roles) +{ + if (getRoles_ == nullptr) { + HDF_LOGE("%{public}s: getRoles is null.", __func__); + return HDF_ERR_INVALID_PARAM; + } + uint32_t roleCount = 0; + uint32_t err = (*getRoles_)(const_cast(name.c_str()), &roleCount, nullptr); + if (err != HDF_SUCCESS) { + HDF_LOGE("%{public}s: getRoles_ return err [%{public}x].", __func__, err); + return err; + } + if (roleCount <= 0) { + return err; + } + + char role[roleCount][OMX_MAX_STRINGNAME_SIZE]; + uint32_t bufferSize = sizeof(char) * (roleCount * OMX_MAX_STRINGNAME_SIZE); + err = memset_s(role, sizeof(role), 0, bufferSize); + if (err != EOK) { + HDF_LOGE("%{public}s: memset_s return err [%{public}x].", __func__, err); + return err; + } + + uint32_t roleLen = roleCount; + err = (*getRoles_)(const_cast(name.c_str()), &roleCount, reinterpret_cast(role)); + if (err != HDF_SUCCESS) { + HDF_LOGE("%{public}s: getRoles_ return err [%{public}x].", __func__, err); + return err; + } + for (uint32_t i = 0; i < roleLen; i++) { + roles.push_back(role[i]); + } + + return err; +} +} // namespace Omx +} // namespace Codec +} // namespace OHOS \ No newline at end of file diff --git a/codec/hal/v2.0/hdi_impl/src/component_mgr.cpp b/codec/hal/v2.0/hdi_impl/src/component_mgr.cpp index 5ecbc8a79b..5db39ad1f0 100644 --- a/codec/hal/v2.0/hdi_impl/src/component_mgr.cpp +++ b/codec/hal/v2.0/hdi_impl/src/component_mgr.cpp @@ -21,28 +21,16 @@ #include #include #define HDF_LOG_TAG codec_hdi_server -constexpr int COMPONENT_NAME_MAX_LEN = 128; namespace OHOS { namespace Codec { namespace Omx { -ComponentMgr::ComponentMgr() : loadLibSuc_(false), currentComType_(NULL), currentComName_("") +ComponentMgr::ComponentMgr() { AddVendorComponent(); AddSoftComponent(); } -bool ComponentMgr::IsOMXHandleValid(OMX_COMPONENTTYPE *handle) -{ - for (size_t i = 0; i < componentTypePointAndObjectPoint_.size(); i++) { - if (handle == componentTypePointAndObjectPoint_[i].componentType) { - return true; - } - } - HDF_LOGE("%{public}s can not find handle [0x%{public}p]", __func__, handle); - return false; -} - ComponentMgr::~ComponentMgr() { CleanComponent(); @@ -51,97 +39,51 @@ ComponentMgr::~ComponentMgr() int32_t ComponentMgr::CreateComponentInstance(const char *componentName, const OMX_CALLBACKTYPE *callbacks, void *appData, OMX_COMPONENTTYPE **component) { - HDF_LOGI("ComponentMgr::CreateComponentInstance:%{public}s", componentName); - *component = NULL; - int32_t err = OMX_ErrorMax; + int32_t err = HDF_ERR_INVALID_PARAM; + std::lock_guard lk(mutex_); - std::vector::iterator it; - for (it = componentNameAndObjectPoint_.begin(); it != componentNameAndObjectPoint_.end(); ++it) { - if (strcmp(componentName, it->componentName.c_str()) != 0) { - continue; - } - if (it->omxComponentMgr == nullptr) { - break; - } - err = it->omxComponentMgr->CreateComponentInstance(componentName, callbacks, appData, component); - std::vector roles; - it->omxComponentMgr->GetRolesForComponent(componentName, &roles); - if (err != OMX_ErrorNone) { - HDF_LOGE("%{public}s CreateComponentInstance error %{public}x", __func__, err); - break; - } - ComponentTypePointAndObjectPoint point; - point.componentType = *component; - point.omxComponentMgr = it->omxComponentMgr; - componentTypePointAndObjectPoint_.push_back(point); - currentComType_ = *component; - currentComName_ = componentName; + auto iter = compoentsCore_.find(componentName); + if (iter == compoentsCore_.end() || iter->second == nullptr) { + HDF_LOGE("%{public}s: can not find component[%{public}s] in core", __func__, componentName); + return err; + } + auto core = iter->second; + OMX_HANDLETYPE handle = nullptr; + std::string name(componentName); + err = core->GetHandle(handle, name, appData, *callbacks); + if (err == OMX_ErrorNone && handle) { + OMXComponent comp; + comp.core = core; + *component = reinterpret_cast(handle); + comp.handle = handle; + components_.push_back(comp); } return err; } int32_t ComponentMgr::DeleteComponentInstance(OMX_COMPONENTTYPE *component) { - std::vector::iterator it; - for (it = componentTypePointAndObjectPoint_.begin(); it != componentTypePointAndObjectPoint_.end(); ++it) { - if (it->componentType == component) { - IComponentMgr *pOMXComponentMgr = it->omxComponentMgr; - componentTypePointAndObjectPoint_.erase(it); - if (pOMXComponentMgr != nullptr) { - return pOMXComponentMgr->DeleteComponentInstance(component); - } - return OMX_ErrorMax; + int32_t err = OMX_ErrorInvalidComponent; + for (size_t i = 0; i < components_.size(); i++) { + if (components_[i].handle == component) { + err = components_[i].core->FreeHandle(components_[i].handle); + components_.erase(components_.begin() + i); + break; } } - return OMX_ErrorMax; -} - -int32_t ComponentMgr::EnumerateComponentsByIndex(uint32_t index, char *componentName, size_t componentNameSize) -{ - size_t componentNum = componentNameAndObjectPoint_.size(); - if (index >= componentNum) { - HDF_LOGE("%{public}s index [%{public}d] > componentNum [%{public}zu]", __func__, index, componentNum); - return OMX_ErrorInvalidComponentName; - } - std::string &compName = componentNameAndObjectPoint_[index].componentName; - if (componentNameSize < compName.length() + 1) { - HDF_LOGE("%{public}s componentNameSize [%{public}d] is too short", __func__, index); - return OMX_ErrorMax; - } - errno_t ret = strcpy_s(componentName, componentNameSize, compName.c_str()); - if (ret != EOK) { - HDF_LOGE("%{public}s strcpy_s return error", __func__); - return OMX_ErrorInsufficientResources; - } - - return OMX_ErrorNone; + return err; } int32_t ComponentMgr::GetRolesForComponent(const char *componentName, std::vector *roles) { - if (roles == NULL) { - return OMX_ErrorMax; - } - roles->clear(); - std::vector::iterator it; - for (it = componentNameAndObjectPoint_.begin(); it != componentNameAndObjectPoint_.end(); ++it) { - if (strcmp(componentName, it->componentName.c_str()) != 0) { - continue; - } - if (it->omxComponentMgr == nullptr) { - HDF_LOGE("%{public}s omxComponentMgr is null", __func__); - return OMX_ErrorInvalidComponentName; - } - - int32_t err = it->omxComponentMgr->GetRolesForComponent(componentName, roles); - return err; - } - return OMX_ErrorInvalidComponentName; + (void)roles; + (void)componentName; + return OMX_ErrorNone; } void ComponentMgr::AddVendorComponent() { - std::string path = HDF_LIBRARY_FULL_PATH("libOMX_Pluginhw"); + std::string path = HDF_LIBRARY_FULL_PATH("libOMX_Core"); AddComponentByLibName(path.c_str()); } @@ -150,72 +92,32 @@ void ComponentMgr::AddSoftComponent() void ComponentMgr::AddComponentByLibName(const char *libName) { - void *libHandle = dlopen(libName, RTLD_LAZY); - if (libHandle == NULL) { - HDF_LOGE("ComponentMgr::AddComponentByLibName:libHandle is NULL, path is %{public}s", libName); - return; - } - typedef IComponentMgr *(*CreateOMXPluginFunc)(); - CreateOMXPluginFunc createOMXPlugin = (CreateOMXPluginFunc)dlsym(libHandle, "createOMXPlugin"); - - IComponentMgr *plugin = nullptr; - if (createOMXPlugin != nullptr) { - plugin = (*createOMXPlugin)(); - } - if (plugin != nullptr) { - ComponentInfo info; - info.omxComponent = plugin; - info.LibHandle = libHandle; - componentsList_.push_back(info); - AddComponentByInstance(plugin); - loadLibSuc_ = true; - } else { - dlclose(libHandle); - } -} - -void ComponentMgr::AddComponentByInstance(IComponentMgr *pMrg) -{ + auto core = std::make_shared(); + core->Init(libName); + std::lock_guard lk(mutex_); + cores_.emplace_back(core); + std::string name(""); uint32_t index = 0; - char name[COMPONENT_NAME_MAX_LEN]; - bool exists = false; - int32_t err; - while ((err = pMrg->EnumerateComponentsByIndex(index++, name, sizeof(name))) == OMX_ErrorNone) { - std::vector::iterator it; - for (it = componentNameAndObjectPoint_.begin(); it != componentNameAndObjectPoint_.end(); ++it) { - if (strcmp(name, it->componentName.c_str()) == 0) { - exists = true; - break; - } - } - if (exists) { - exists = false; - continue; - } - HDF_LOGI("ComponentMgr::AddComponentByInstance:component name=%{public}s", name); - ComponentNameAndObjectPoint point; - point.componentName = name; - point.omxComponentMgr = pMrg; - componentNameAndObjectPoint_.push_back(point); + while (HDF_SUCCESS == core->ComponentNameEnum(name, index)) { + ++index; + compoentsCore_.emplace(std::make_pair(name, core)); } } void ComponentMgr::CleanComponent() { - componentNameAndObjectPoint_.clear(); - componentTypePointAndObjectPoint_.clear(); + std::lock_guard lk(mutex_); + for (size_t i = 0; i < components_.size(); i++) { + components_[i].core->FreeHandle(components_[i].handle); + } + components_.clear(); - typedef void (*DestroyOMXPluginFunc)(IComponentMgr *); - for (const ComponentInfo &comInfo : componentsList_) { - DestroyOMXPluginFunc destroyOMXPlugin = (DestroyOMXPluginFunc)dlsym(comInfo.LibHandle, "destroyOMXPlugin"); - if (destroyOMXPlugin != nullptr) { - destroyOMXPlugin(comInfo.omxComponent); - } else { - delete comInfo.omxComponent; - } - dlclose(comInfo.LibHandle); + for (size_t i = 0; i < cores_.size(); i++) { + cores_[i]->DeInit(); } - componentsList_.clear(); + cores_.clear(); + + compoentsCore_.clear(); } } // namespace Omx } // namespace Codec -- Gitee