1 /*
2  * Copyright (c) 2022-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 
17 #include "rs_frame_report.h"
18 
19 #include <dlfcn.h>
20 #include <cstdio>
21 #include <unistd.h>
22 
23 #include "hilog/log.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 #undef LOG_DOMAIN
28 #define LOG_DOMAIN 0xD001400
29 
30 #undef LOG_TAG
31 #define LOG_TAG "OHOS::RS"
32 #define LOGI(fmt, ...) HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__)
33 #define LOGE(fmt, ...) HILOG_ERROR(LOG_CORE, fmt, ##__VA_ARGS__)
34 namespace {
35 const std::string FRAME_AWARE_SO_PATH = "libframe_ui_intf.z.so";
36 }
GetInstance()37 RsFrameReport& RsFrameReport::GetInstance()
38 {
39     static RsFrameReport instance;
40     return instance;
41 }
42 
RsFrameReport()43 RsFrameReport::RsFrameReport() {}
44 
~RsFrameReport()45 RsFrameReport::~RsFrameReport()
46 {
47     CloseLibrary();
48 }
49 
Init()50 void RsFrameReport::Init()
51 {
52     int ret = LoadLibrary();
53     if (!ret) {
54         LOGE("RsFrameReport:[Init] dlopen libframe_ui_intf.so failed!");
55         return;
56     }
57     LOGI("RsFrameReport:[Init] dlopen libframe_ui_intf.so success!");
58     initFunc_ = (InitFunc)LoadSymbol("Init");
59     if (initFunc_ != nullptr) {
60         initFunc_();
61     }
62 }
63 
LoadLibrary()64 bool RsFrameReport::LoadLibrary()
65 {
66     if (!frameSchedSoLoaded_) {
67         frameSchedHandle_ = dlopen(FRAME_AWARE_SO_PATH.c_str(), RTLD_LAZY);
68         if (frameSchedHandle_ == nullptr) {
69             LOGE("RsFrameReport:[LoadLibrary]dlopen libframe_ui_intf.so failed!"
70                 " error = %{public}s\n", dlerror());
71             return false;
72         }
73         frameSchedSoLoaded_ = true;
74     }
75     LOGI("RsFrameReport:[LoadLibrary] load library success!");
76     return true;
77 }
78 
CloseLibrary()79 void RsFrameReport::CloseLibrary()
80 {
81     if (dlclose(frameSchedHandle_) != 0) {
82         LOGE("RsFrameReport:[CloseLibrary]libframe_ui_intf.so failed!\n");
83         return;
84     }
85     frameSchedHandle_ = nullptr;
86     frameSchedSoLoaded_ = false;
87     LOGI("RsFrameReport:[CloseLibrary]libframe_ui_intf.so close success!\n");
88 }
89 
LoadSymbol(const char * symName)90 void *RsFrameReport::LoadSymbol(const char *symName)
91 {
92     if (!frameSchedSoLoaded_) {
93         LOGE("RsFrameReport:[loadSymbol]libframe_ui_intf.so not loaded.\n");
94         return nullptr;
95     }
96     void *funcSym = dlsym(frameSchedHandle_, symName);
97     if (funcSym == nullptr) {
98         LOGE("RsFrameReport:[loadSymbol]Get %{public}s symbol failed: %{public}s\n", symName, dlerror());
99         return nullptr;
100     }
101     return funcSym;
102 }
103 
GetEnable()104 int RsFrameReport::GetEnable()
105 {
106     if (!frameSchedSoLoaded_) {
107         return 0;
108     }
109     if (frameGetEnableFunc_ == nullptr) {
110         frameGetEnableFunc_ = (FrameGetEnableFunc)LoadSymbol("GetSenseSchedEnable");
111     }
112     if (frameGetEnableFunc_ != nullptr) {
113         return frameGetEnableFunc_();
114     } else {
115         LOGE("RsFrameReport:[GetEnable]load GetSenseSchedEnable function failed!");
116         return 0;
117     }
118 }
119 
ProcessCommandsStart()120 void RsFrameReport::ProcessCommandsStart()
121 {
122     if (processCommandsStartFun_ == nullptr) {
123         processCommandsStartFun_ = (ProcessCommandsStartFunc)LoadSymbol("ProcessCommandsStart");
124     }
125     if (processCommandsStartFun_ != nullptr) {
126         processCommandsStartFun_();
127     } else {
128         LOGE("RsFrameReport:[ProcessCommandsStart]load ProcessCommandsStart function failed!");
129     }
130 }
131 
AnimateStart()132 void RsFrameReport::AnimateStart()
133 {
134     if (animateStartFunc_ == nullptr) {
135         animateStartFunc_ = (AnimateStartFunc)LoadSymbol("AnimateStart");
136     }
137     if (animateStartFunc_ != nullptr) {
138         animateStartFunc_();
139     } else {
140         LOGE("RsFrameReport:[AnimateStart]load AnimateStart function failed!");
141     }
142 }
143 
RenderStart(uint64_t timestamp)144 void RsFrameReport::RenderStart(uint64_t timestamp)
145 {
146     if (renderStartFunc_ == nullptr) {
147         renderStartFunc_ = (RenderStartFunc)LoadSymbol("RenderStart");
148     }
149     if (renderStartFunc_ != nullptr) {
150         renderStartFunc_(timestamp);
151     } else {
152         LOGE("RsFrameReport:[RenderStart]load RenderStart function failed!");
153     }
154 }
155 
RSRenderStart()156 void RsFrameReport::RSRenderStart()
157 {
158     if (parallelRenderStartFunc_ == nullptr) {
159         parallelRenderStartFunc_ = (ParallelRenderStartFunc)LoadSymbol("RSRenderStart");
160     }
161     if (parallelRenderStartFunc_ != nullptr) {
162         parallelRenderStartFunc_();
163     } else {
164         LOGE("RsFrameReport:[RSRenderStart]load RSRenderStart function failed!");
165     }
166 }
167 
RenderEnd()168 void RsFrameReport::RenderEnd()
169 {
170     if (renderEndFunc_ == nullptr) {
171         renderEndFunc_ = (RenderEndFunc)LoadSymbol("RenderEnd");
172     }
173     if (renderEndFunc_ != nullptr) {
174         renderEndFunc_();
175     } else {
176         LOGE("RsFrameReport:[RenderEnd]load RenderEnd function failed!");
177     }
178 }
179 
RSRenderEnd()180 void RsFrameReport::RSRenderEnd()
181 {
182     if (parallelRenderEndFunc_ == nullptr) {
183         parallelRenderEndFunc_ = (ParallelRenderEndFunc)LoadSymbol("RSRenderEnd");
184     }
185     if (parallelRenderEndFunc_ != nullptr) {
186         parallelRenderEndFunc_();
187     } else {
188         LOGE("RsFrameReport:[RSRenderEnd]load RSRenderEnd function failed!");
189     }
190 }
191 
SendCommandsStart()192 void RsFrameReport::SendCommandsStart()
193 {
194     if (sendCommandsStartFunc_ == nullptr) {
195         sendCommandsStartFunc_ = (SendCommandsStartFunc)LoadSymbol("SendCommandsStart");
196     }
197     if (sendCommandsStartFunc_ != nullptr) {
198         sendCommandsStartFunc_();
199     } else {
200         LOGE("RsFrameReport:[SendCommandsStart]load SendCommandsStart function failed!");
201     }
202 }
203 
SetFrameParam(int requestId,int load,int schedFrameNum,int value)204 void RsFrameReport::SetFrameParam(int requestId, int load, int schedFrameNum, int value)
205 {
206     if (setFrameParamFunc_ == nullptr) {
207         setFrameParamFunc_ = (SetFrameParamFunc)LoadSymbol("SetFrameParam");
208     }
209 
210     if (setFrameParamFunc_ != nullptr) {
211         setFrameParamFunc_(requestId, load, schedFrameNum, value);
212     } else {
213         LOGE("RsFrameReport:[SetFrameParam]load SetFrameParam function failed");
214     }
215 }
216 } // namespace Rosen
217 } // namespace OHOS
218