1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "external_loader.h"
17 #include "effect_trace.h"
18
19 #include <dlfcn.h>
20
21 namespace OHOS {
22 namespace Media {
23 namespace Effect {
Instance()24 ExternLoader *ExternLoader::Instance()
25 {
26 static ExternLoader instance;
27 return &instance;
28 }
29
LoadExtSo()30 void ExternLoader::LoadExtSo()
31 {
32 EFFECT_TRACE_NAME("ExternLoader::LoadExtSo");
33 std::unique_lock<std::mutex> lock(loadExtSo_);
34 EFFECT_LOGI("EFilterFactory:LoadExtSo enter!");
35 if (isExtLoad_) {
36 return;
37 }
38 void *effectExtHandle = dlopen("libimage_effect_ext.so", RTLD_NOW);
39 if (effectExtHandle == nullptr) {
40 EFFECT_LOGE("EFilterFactory: dlopen libimage_effect_ext.so failed! dlerror=%{public}s", dlerror());
41 isExtLoad_ = false;
42 return;
43 }
44 EFFECT_LOGI("EFilterFactory: dlopen libimage_effect_ext.so success!");
45 initFunc_ = reinterpret_cast<InitModuleFunc>(dlsym(effectExtHandle, "Init"));
46 deinitFunc_ = reinterpret_cast<InitModuleFunc>(dlsym(effectExtHandle, "Deinit"));
47 initModuleFunc_ = reinterpret_cast<InitModuleFunc>(dlsym(effectExtHandle, "InitModule"));
48 deinitModuleFunc_ = reinterpret_cast<InitModuleFunc>(dlsym(effectExtHandle, "DeinitModule"));
49 isExtLoad_ = true;
50 }
51
IsExtLoad() const52 bool ExternLoader::IsExtLoad() const
53 {
54 return isExtLoad_;
55 }
56
GetInitFunc() const57 InitFunc ExternLoader::GetInitFunc() const
58 {
59 return initFunc_;
60 }
61
GetDeinitFunc() const62 InitFunc ExternLoader::GetDeinitFunc() const
63 {
64 return deinitFunc_;
65 }
66
GetInitModuleFunc() const67 InitModuleFunc ExternLoader::GetInitModuleFunc() const
68 {
69 return initModuleFunc_;
70 }
71
GetDeinitModuleFunc() const72 InitModuleFunc ExternLoader::GetDeinitModuleFunc() const
73 {
74 return deinitModuleFunc_;
75 }
76
InitExt()77 void ExternLoader::InitExt()
78 {
79 std::unique_lock<std::mutex> lock(initExtSo_);
80 if (!IsExtLoad()) {
81 LoadExtSo();
82 }
83
84 if (hasInitExt_) {
85 return;
86 }
87
88 auto initFunc = ExternLoader::Instance()->GetInitFunc();
89 if (initFunc) {
90 initFunc();
91 } else {
92 EFFECT_LOGE("EFilterFactory: shared lib so not find function!");
93 }
94 hasInitExt_ = true;
95 }
96 } // namespace Effect
97 } // namespace Media
98 } // namespace OHOS