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 "frame_sched.h"
17 
18 #include <dlfcn.h>
19 #include <cstdio>
20 #include <securec.h>
21 #include <unistd.h>
22 #include <hilog/log.h>
23 
24 namespace OHOS {
25 namespace Rosen {
26 
27 static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, 0xD001404, "FrameSched" };
28 #define LOGF(...) (void)OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, __VA_ARGS__)
29 #define LOGE(...) (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, __VA_ARGS__)
30 #define LOGW(...) (void)OHOS::HiviewDFX::HiLog::Warn(LOG_LABEL, __VA_ARGS__)
31 #define LOGI(...) (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, __VA_ARGS__)
32 #define LOGD(...) (void)OHOS::HiviewDFX::HiLog::Debug(LOG_LABEL, __VA_ARGS__)
33 
34 const std::string FRAME_AWARE_SO_PATH = "libframe_ui_intf.z.so";
35 
GetInstance()36 FrameSched& FrameSched::GetInstance()
37 {
38     static FrameSched instance;
39     return instance;
40 }
41 
FrameSched()42 FrameSched::FrameSched()
43 {
44     LoadLibrary();
45 }
46 
~FrameSched()47 FrameSched::~FrameSched()
48 {
49     CloseLibrary();
50 }
51 
LoadLibrary()52 bool FrameSched::LoadLibrary()
53 {
54     if (!schedSoLoaded_) {
55         schedHandle_ = dlopen(FRAME_AWARE_SO_PATH.c_str(), RTLD_LAZY);
56         if (schedHandle_ == nullptr) {
57             LOGE("dlopen libframe_ui_intf.so failed! error = %{public}s", dlerror());
58             return false;
59         }
60         schedSoLoaded_ = true;
61     }
62     LOGI("load library success!");
63     return true;
64 }
65 
CloseLibrary()66 void FrameSched::CloseLibrary()
67 {
68     if (schedHandle_ != nullptr) {
69         if (dlclose(schedHandle_) != 0) {
70             LOGE("libframe_ui_intf.so close failed!\n");
71             return;
72         }
73     }
74     schedHandle_ = nullptr;
75     schedSoLoaded_ = false;
76     LOGI("libframe_ui_intf.so close success!\n");
77 }
78 
LoadSymbol(const char * symName)79 void* FrameSched::LoadSymbol(const char* symName)
80 {
81     if (!schedSoLoaded_) {
82         LOGE("libframe_ui_intf.so not loaded.\n");
83         return nullptr;
84     }
85 
86     void *funcSym = dlsym(schedHandle_, symName);
87     if (funcSym == nullptr) {
88         LOGE("Get %{public}s symbol failed: %{public}s\n", symName, dlerror());
89         return nullptr;
90     }
91     return funcSym;
92 }
93 
Init()94 void FrameSched::Init()
95 {
96     if (initFunc_ == nullptr) {
97         initFunc_ = (InitFunc)LoadSymbol("Init");
98     }
99     if (initFunc_ != nullptr) {
100         initFunc_();
101     } else {
102         LOGE("FrameSched:[Init]load Init function failed.");
103     }
104 }
105 
MonitorGpuStart()106 void FrameSched::MonitorGpuStart()
107 {
108     if (monitorGpuStartFunc_ == nullptr) {
109         monitorGpuStartFunc_ = (MonitorGpuStartFunc)LoadSymbol("MonitorGpuStart");
110     }
111     if (monitorGpuStartFunc_ != nullptr) {
112         monitorGpuStartFunc_();
113     } else {
114         LOGE("FrameSched:[MonitorStart]load MonitorStart function failed.");
115     }
116 }
117 
MonitorGpuEnd()118 void FrameSched::MonitorGpuEnd()
119 {
120     if (monitorGpuEndFunc_ == nullptr) {
121         monitorGpuEndFunc_ = (MonitorGpuEndFunc)LoadSymbol("MonitorGpuEnd");
122     }
123     if (monitorGpuEndFunc_ != nullptr) {
124         monitorGpuEndFunc_();
125     } else {
126         LOGE("FrameSched:[MonitorEnd]load MonitorEnd function failed.");
127     }
128 }
129 
SetFrameParam(int requestId,int load,int schedFrameNum,int value)130 void FrameSched::SetFrameParam(int requestId, int load, int schedFrameNum, int value)
131 {
132     if (setFrameParamFunc_ == nullptr) {
133         setFrameParamFunc_ = (SetFrameParamFunc)LoadSymbol("SetFrameParam");
134     }
135     if (setFrameParamFunc_ != nullptr) {
136         setFrameParamFunc_(requestId, load, schedFrameNum, value);
137     } else {
138         LOGE("FrameSched:[SetFrameParam]load MonitorEnd function failed.");
139     }
140 }
141 } // namespace Rosen
142 } // namespace OHOS