123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#include <dlfcn.h> 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci#include "frameworks/bridge/common/utils/engine_helper.h" 1923b3eb3cSopenharmony_ci#include "core/common/ai/image_analyzer_loader.h" 2023b3eb3cSopenharmony_ci 2123b3eb3cSopenharmony_cinamespace OHOS::Ace { 2223b3eb3cSopenharmony_cinamespace { 2323b3eb3cSopenharmony_ci#ifdef __aarch64__ 2423b3eb3cSopenharmony_ciconstexpr char IMAGE_ANALYZER_SO_PATH[] = "system/lib64/libai_image_analyzer_innerapi.z.so"; 2523b3eb3cSopenharmony_ci#else 2623b3eb3cSopenharmony_ciconstexpr char IMAGE_ANALYZER_SO_PATH[] = "system/lib/libai_image_analyzer_innerapi.z.so"; 2723b3eb3cSopenharmony_ci#endif 2823b3eb3cSopenharmony_ci} // namespace 2923b3eb3cSopenharmony_ci 3023b3eb3cSopenharmony_cistd::shared_ptr<ImageAnalyzerLoader> ImageAnalyzerLoader::Load() 3123b3eb3cSopenharmony_ci{ 3223b3eb3cSopenharmony_ci auto engLib(std::make_shared<ImageAnalyzerLoader>()); 3323b3eb3cSopenharmony_ci return engLib->Init() ? engLib : nullptr; 3423b3eb3cSopenharmony_ci} 3523b3eb3cSopenharmony_ci 3623b3eb3cSopenharmony_ciImageAnalyzerLoader::~ImageAnalyzerLoader() 3723b3eb3cSopenharmony_ci{ 3823b3eb3cSopenharmony_ci Close(); 3923b3eb3cSopenharmony_ci} 4023b3eb3cSopenharmony_ci 4123b3eb3cSopenharmony_cibool ImageAnalyzerLoader::Init() 4223b3eb3cSopenharmony_ci{ 4323b3eb3cSopenharmony_ci libraryHandle_ = dlopen(IMAGE_ANALYZER_SO_PATH, RTLD_LAZY); 4423b3eb3cSopenharmony_ci if (libraryHandle_ == nullptr) { 4523b3eb3cSopenharmony_ci TAG_LOGE(AceLogTag::ACE_IMAGE, " Could not dlopen %s: %s", IMAGE_ANALYZER_SO_PATH, dlerror()); 4623b3eb3cSopenharmony_ci return false; 4723b3eb3cSopenharmony_ci } 4823b3eb3cSopenharmony_ci createImageAnalyzerInstance_ = 4923b3eb3cSopenharmony_ci (ImageAnalyzerInterface* (*)(napi_env)) dlsym(libraryHandle_, "createImageAnalyzerInstance"); 5023b3eb3cSopenharmony_ci destroyImageAnalyzerInstance_ = 5123b3eb3cSopenharmony_ci (void (*)(ImageAnalyzerInterface *)) dlsym(libraryHandle_, "destroyImageAnalyzerInstance"); 5223b3eb3cSopenharmony_ci if (createImageAnalyzerInstance_ == nullptr || destroyImageAnalyzerInstance_ == nullptr) { 5323b3eb3cSopenharmony_ci TAG_LOGE(AceLogTag::ACE_IMAGE, " Could not find engine interface functions in %s", IMAGE_ANALYZER_SO_PATH); 5423b3eb3cSopenharmony_ci Close(); 5523b3eb3cSopenharmony_ci return false; 5623b3eb3cSopenharmony_ci } 5723b3eb3cSopenharmony_ci return true; 5823b3eb3cSopenharmony_ci} 5923b3eb3cSopenharmony_ci 6023b3eb3cSopenharmony_ciImageAnalyzerInstance ImageAnalyzerLoader::CreateImageAnalyzer() 6123b3eb3cSopenharmony_ci{ 6223b3eb3cSopenharmony_ci if (createImageAnalyzerInstance_ == nullptr || destroyImageAnalyzerInstance_ == nullptr) { 6323b3eb3cSopenharmony_ci return {}; 6423b3eb3cSopenharmony_ci } 6523b3eb3cSopenharmony_ci auto engine = OHOS::Ace::EngineHelper::GetCurrentEngine(); 6623b3eb3cSopenharmony_ci NativeEngine* nativeEngine = engine->GetNativeEngine(); 6723b3eb3cSopenharmony_ci auto env = reinterpret_cast<napi_env>(nativeEngine); 6823b3eb3cSopenharmony_ci return { createImageAnalyzerInstance_(env), [lib = shared_from_this(), 6923b3eb3cSopenharmony_ci destroy = destroyImageAnalyzerInstance_](ImageAnalyzerInterface *e) { destroy(e); } }; 7023b3eb3cSopenharmony_ci} 7123b3eb3cSopenharmony_ci 7223b3eb3cSopenharmony_civoid ImageAnalyzerLoader::Close() 7323b3eb3cSopenharmony_ci{ 7423b3eb3cSopenharmony_ci if (libraryHandle_ != nullptr) { 7523b3eb3cSopenharmony_ci dlclose(libraryHandle_); 7623b3eb3cSopenharmony_ci } 7723b3eb3cSopenharmony_ci libraryHandle_ = nullptr; 7823b3eb3cSopenharmony_ci createImageAnalyzerInstance_ = nullptr; 7923b3eb3cSopenharmony_ci destroyImageAnalyzerInstance_ = nullptr; 8023b3eb3cSopenharmony_ci} 8123b3eb3cSopenharmony_ci} 82