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
18 #include <dlfcn.h>
19
20 namespace OHOS {
21 namespace Media {
22 namespace Effect {
Instance()23 ExternLoader *ExternLoader::Instance()
24 {
25 static ExternLoader instance;
26 return &instance;
27 }
28
LoadExtSo()29 void ExternLoader::LoadExtSo()
30 {
31 std::unique_lock<std::mutex> lock(loadExtSo_);
32 EFFECT_LOGI("EFilterFactory:LoadExtSo enter!");
33 if (isExtLoad_) {
34 return;
35 }
36 void *effectExtHandle = dlopen("libimage_effect_ext.so", RTLD_NOW);
37 if (effectExtHandle == nullptr) {
38 EFFECT_LOGE("EFilterFactory: dlopen libimage_effect_ext.so failed! dlerror=%{public}s", dlerror());
39 isExtLoad_ = false;
40 return;
41 }
42 EFFECT_LOGI("EFilterFactory: dlopen libimage_effect_ext.so success!");
43
44 createImageEffectExtFunc_ = reinterpret_cast<CreateImageEffectExtFunc>(dlsym(effectExtHandle,
45 "CreateImageEffectEXT"));
46 initFunc_ = reinterpret_cast<InitModuleFunc>(dlsym(effectExtHandle, "Init"));
47 deinitFunc_ = reinterpret_cast<InitModuleFunc>(dlsym(effectExtHandle, "Deinit"));
48 initModuleFunc_ = reinterpret_cast<InitModuleFunc>(dlsym(effectExtHandle, "InitModule"));
49 deinitModuleFunc_ = reinterpret_cast<InitModuleFunc>(dlsym(effectExtHandle, "DeinitModule"));
50 isExtLoad_ = true;
51 }
52
IsExtLoad() const53 bool ExternLoader::IsExtLoad() const
54 {
55 return isExtLoad_;
56 }
57
GetCreateImageEffectExtFunc() const58 CreateImageEffectExtFunc ExternLoader::GetCreateImageEffectExtFunc() const
59 {
60 return createImageEffectExtFunc_;
61 }
62
GetInitFunc() const63 InitFunc ExternLoader::GetInitFunc() const
64 {
65 return initFunc_;
66 }
67
GetDeinitFunc() const68 InitFunc ExternLoader::GetDeinitFunc() const
69 {
70 return deinitFunc_;
71 }
72
GetInitModuleFunc() const73 InitModuleFunc ExternLoader::GetInitModuleFunc() const
74 {
75 return initModuleFunc_;
76 }
77
GetDeinitModuleFunc() const78 InitModuleFunc ExternLoader::GetDeinitModuleFunc() const
79 {
80 return deinitModuleFunc_;
81 }
82
InitExt()83 void ExternLoader::InitExt()
84 {
85 std::unique_lock<std::mutex> lock(initExtSo_);
86 if (!IsExtLoad()) {
87 LoadExtSo();
88 }
89
90 if (hasInitExt_) {
91 return;
92 }
93
94 auto initFunc = ExternLoader::Instance()->GetInitFunc();
95 if (initFunc) {
96 initFunc();
97 } else {
98 EFFECT_LOGE("EFilterFactory: shared lib so not find function!");
99 }
100 hasInitExt_ = true;
101 }
102 } // namespace Effect
103 } // namespace Media
104 } // namespace OHOS