1 /*
2 * Copyright (c) 2025 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 #include "ge_external_dynamic_loader.h"
16 #include "ge_log.h"
17 #include "ge_system_properties.h"
18
19 #ifdef GE_PLATFORM_UNIX
20 #include <dlfcn.h>
21 #endif
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26 const std::string GRAPHICS_EFFECT_EXT_ENABLE = "rosen.graphic.gex.enable";
27 #ifdef GE_PLATFORM_UNIX
28 #if (defined(__aarch64__) || defined(__x86_64__))
29 const std::string GRAPHICS_EFFECT_EXT_LIB_PATH = "/system/lib64/libgraphics_effect_ext.z.so";
30 #else
31 const std::string GRAPHICS_EFFECT_EXT_LIB_PATH = "/system/lib/libgraphics_effect_ext.z.so";
32 #endif
33 const std::string GRAPHICS_EFFECT_EXT_INREFACE = "CreateGEXObjectByType";
34 #endif
35 }
36
GEExternalDynamicLoader()37 GEExternalDynamicLoader::GEExternalDynamicLoader()
38 {
39 LOGI("GEExternalDynamicLoader load");
40 #ifdef GE_PLATFORM_UNIX
41 libHandle_ = dlopen(GRAPHICS_EFFECT_EXT_LIB_PATH.c_str(), RTLD_LAZY);
42 if (!libHandle_) {
43 LOGE("GEExternalDynamicLoader lib handle is null");
44 return;
45 }
46
47 createObjectFunc_ = (CreateGEXObjectByTypeFunc)dlsym(libHandle_, GRAPHICS_EFFECT_EXT_INREFACE.c_str());
48 if (!createObjectFunc_) {
49 LOGE("GEExternalDynamicLoader CreateObjectFunc is null");
50 return;
51 }
52 LOGI("GEExternalDynamicLoader load success");
53 #endif
54 }
55
~GEExternalDynamicLoader()56 GEExternalDynamicLoader::~GEExternalDynamicLoader()
57 {
58 LOGI("GEExternalDynamicLoader unload");
59 #ifdef GE_PLATFORM_UNIX
60 if (libHandle_) {
61 dlclose(libHandle_);
62 }
63 #endif
64 libHandle_ = nullptr;
65 }
66
GetInstance()67 GEExternalDynamicLoader& GEExternalDynamicLoader::GetInstance()
68 {
69 static GEExternalDynamicLoader instance;
70 return instance;
71 }
72
CreateGEXObjectByType(uint32_t type,uint32_t len,void * param)73 void* GEExternalDynamicLoader::CreateGEXObjectByType(uint32_t type, uint32_t len, void* param)
74 {
75 if (!createObjectFunc_) {
76 LOGD("GEExternalDynamicLoader::CreateGEXObjectByType interface is null");
77 return nullptr;
78 }
79
80 #ifdef GE_OHOS
81 auto enable = system::GetBoolParameter(GRAPHICS_EFFECT_EXT_ENABLE, true);
82 if (enable) {
83 return createObjectFunc_(type, len, param);
84 }
85
86 LOGW("GEExternalDynamicLoader::CreateGEXObjectByType dynamic load disabled");
87 return nullptr;
88 #else
89 return createObjectFunc_(type, len, param);
90 #endif
91 }
92
93 } // namespace Rosen
94 } // namespace OHOS
95