1 /*
2  * Copyright (c) 2022 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 "base/log/frame_report.h"
17 
18 #include <cstdio>
19 #include <dlfcn.h>
20 #include <unistd.h>
21 
22 #include <frame_collector.h>
23 
24 #include "base/log/log_wrapper.h"
25 #include "base/utils/utils.h"
26 
27 namespace OHOS::Ace {
28 namespace {
29 const std::string FRAME_AWARE_SO_PATH = "libframe_ui_intf.z.so";
30 } // namespace
GetInstance()31 FrameReport& FrameReport::GetInstance()
32 {
33     static FrameReport instance;
34     return instance;
35 }
36 
FrameReport()37 FrameReport::FrameReport() {}
38 
~FrameReport()39 FrameReport::~FrameReport()
40 {
41     CloseLibrary();
42 }
43 
LoadLibrary()44 bool FrameReport::LoadLibrary()
45 {
46     if (!frameSchedSoLoaded_) {
47         frameSchedHandle_ = dlopen(FRAME_AWARE_SO_PATH.c_str(), RTLD_LAZY);
48         CHECK_NULL_RETURN(frameSchedHandle_, false);
49         frameInitFunc_ = (FrameInitFunc)LoadSymbol("Init");
50         CHECK_NULL_RETURN(frameInitFunc_, false);
51         frameGetEnableFunc_ = (FrameGetEnableFunc)LoadSymbol("GetSenseSchedEnable");
52         CHECK_NULL_RETURN(frameGetEnableFunc_, false);
53         beginFlushAnimationFunc_ = (BeginFlushAnimationFunc)LoadSymbol("BeginFlushAnimation");
54         CHECK_NULL_RETURN(beginFlushAnimationFunc_, false);
55         endFlushAnimationFunc_ = (EndFlushAnimationFunc)LoadSymbol("EndFlushAnimation");
56         CHECK_NULL_RETURN(endFlushAnimationFunc_, false);
57         beginFlushBuildFunc_ = (BeginFlushBuildFunc)LoadSymbol("BeginFlushBuild");
58         CHECK_NULL_RETURN(beginFlushBuildFunc_, false);
59         endFlushBuildFunc_ = (EndFlushBuildFunc)LoadSymbol("EndFlushBuild");
60         CHECK_NULL_RETURN(endFlushBuildFunc_, false);
61         beginFlushLayoutFunc_ = (BeginFlushLayoutFunc)LoadSymbol("BeginFlushLayout");
62         CHECK_NULL_RETURN(beginFlushLayoutFunc_, false);
63         endFlushLayoutFunc_ = (EndFlushLayoutFunc)LoadSymbol("EndFlushLayout");
64         CHECK_NULL_RETURN(endFlushLayoutFunc_, false);
65         beginFlushRenderFunc_ = (BeginFlushRenderFunc)LoadSymbol("BeginFlushRender");
66         CHECK_NULL_RETURN(beginFlushRenderFunc_, false);
67         endFlushRenderFunc_ = (EndFlushRenderFunc)LoadSymbol("EndFlushRender");
68         CHECK_NULL_RETURN(endFlushRenderFunc_, false);
69         beginFlushRenderFinishFunc_ = (BeginFlushRenderFinishFunc)LoadSymbol("BeginFlushRenderFinish");
70         CHECK_NULL_RETURN(beginFlushRenderFinishFunc_, false);
71         endFlushRenderFinishFunc_ = (EndFlushRenderFinishFunc)LoadSymbol("EndFlushRenderFinish");
72         CHECK_NULL_RETURN(endFlushRenderFinishFunc_, false);
73         beginProcessPostFunc_ = (BeginProcessPostFlushFunc)LoadSymbol("BeginProcessPostFlush");
74         CHECK_NULL_RETURN(beginProcessPostFunc_, false);
75         beginListFlingFunc_ = (BeginListFlingFunc)LoadSymbol("BeginListFling");
76         CHECK_NULL_RETURN(beginListFlingFunc_, false);
77         endListFlingFunc_ = (EndListFlingFunc)LoadSymbol("EndListFling");
78         CHECK_NULL_RETURN(endListFlingFunc_, false);
79         flushBeginFunc_ = (FlushBeginFunc)LoadSymbol("FlushBegin");
80         CHECK_NULL_RETURN(flushBeginFunc_, false);
81         flushEndFunc_ = (FlushEndFunc)LoadSymbol("FlushEnd");
82         CHECK_NULL_RETURN(flushEndFunc_, false);
83         setFrameParamFunc_ = (SetFrameParamFunc)LoadSymbol("SetFrameParam");
84         CHECK_NULL_RETURN(setFrameParamFunc_, false);
85         enableSelfRenderFunc_ = (EnableSelfRenderFunc)LoadSymbol("EnableSelfRender");
86         CHECK_NULL_RETURN(enableSelfRenderFunc_, false);
87         disableSelfRenderFunc_ = (DisableSelfRenderFunc)LoadSymbol("DisableSelfRender");
88         CHECK_NULL_RETURN(disableSelfRenderFunc_, false);
89         frameSchedSoLoaded_ = true;
90     }
91     return true;
92 }
93 
CloseLibrary()94 void FrameReport::CloseLibrary()
95 {
96     if (dlclose(frameSchedHandle_) != 0) {
97         LOGE("frame-ace:[CloseLibrary]libframe_ui_intf.so failed!\n");
98         return;
99     }
100     frameSchedHandle_ = nullptr;
101     frameSchedSoLoaded_ = false;
102 }
103 
LoadSymbol(const char * symName)104 void* FrameReport::LoadSymbol(const char* symName)
105 {
106     CHECK_NULL_RETURN(frameSchedHandle_, nullptr);
107     return dlsym(frameSchedHandle_, symName);
108 }
109 
Init()110 void FrameReport::Init()
111 {
112     if (LoadLibrary()) {
113         frameInitFunc_();
114         enable_ = frameGetEnableFunc_() != 0;
115     }
116 }
117 
GetEnable()118 int FrameReport::GetEnable()
119 {
120     return true;
121 }
122 
GetFrameReportEnable()123 int FrameReport::GetFrameReportEnable()
124 {
125     if (!frameSchedSoLoaded_) {
126         return 0;
127     }
128     return frameGetEnableFunc_();
129 }
130 
BeginFlushAnimation()131 void FrameReport::BeginFlushAnimation()
132 {
133     Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::AnimateStart);
134     if (!enable_) {
135         return;
136     }
137     beginFlushAnimationFunc_();
138 }
139 
EndFlushAnimation()140 void FrameReport::EndFlushAnimation()
141 {
142     Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::AnimateEnd);
143     if (!enable_) {
144         return;
145     }
146     endFlushAnimationFunc_();
147 }
148 
BeginFlushBuild()149 void FrameReport::BeginFlushBuild()
150 {
151     Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::BuildStart);
152     if (!enable_) {
153         return;
154     }
155     beginFlushBuildFunc_();
156 }
157 
EndFlushBuild()158 void FrameReport::EndFlushBuild()
159 {
160     Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::BuildEnd);
161     if (!enable_) {
162         return;
163     }
164     endFlushBuildFunc_();
165 }
166 
BeginFlushLayout()167 void FrameReport::BeginFlushLayout()
168 {
169     Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::LayoutStart);
170     if (!enable_) {
171         return;
172     }
173     beginFlushLayoutFunc_();
174 }
175 
EndFlushLayout()176 void FrameReport::EndFlushLayout()
177 {
178     Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::LayoutEnd);
179     if (!enable_) {
180         return;
181     }
182     endFlushLayoutFunc_();
183 }
184 
BeginFlushRender()185 void FrameReport::BeginFlushRender()
186 {
187     Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::DrawStart);
188     if (!enable_) {
189         return;
190     }
191     beginFlushRenderFunc_();
192 }
193 
EndFlushRender()194 void FrameReport::EndFlushRender()
195 {
196     if (!enable_) {
197         return;
198     }
199     endFlushRenderFunc_();
200 }
201 
BeginFlushRenderFinish()202 void FrameReport::BeginFlushRenderFinish()
203 {
204     if (!enable_) {
205         return;
206     }
207     beginFlushRenderFinishFunc_();
208 }
209 
EndFlushRenderFinish()210 void FrameReport::EndFlushRenderFinish()
211 {
212     Rosen::FrameCollector::GetInstance().MarkFrameEvent(Rosen::FrameEventType::DrawEnd);
213     if (!enable_) {
214         return;
215     }
216     endFlushRenderFinishFunc_();
217 }
218 
BeginProcessPostFlush()219 void FrameReport::BeginProcessPostFlush()
220 {
221     if (!enable_) {
222         return;
223     }
224     beginProcessPostFunc_();
225 }
226 
BeginListFling()227 void FrameReport::BeginListFling()
228 {
229     if (!enable_) {
230         return;
231     }
232     beginListFlingFunc_();
233 }
234 
EndListFling()235 void FrameReport::EndListFling()
236 {
237     if (!enable_) {
238         return;
239     }
240     endListFlingFunc_();
241 }
242 
FlushBegin()243 void FrameReport::FlushBegin()
244 {
245     if (!enable_) {
246         return;
247     }
248     flushBeginFunc_();
249 }
250 
FlushEnd()251 void FrameReport::FlushEnd()
252 {
253     if (!enable_) {
254         return;
255     }
256     flushEndFunc_();
257 }
258 
SetFrameParam(int requestId,int load,int schedFrameNum,int value)259 void FrameReport::SetFrameParam(int requestId, int load, int schedFrameNum, int value)
260 {
261     if (!enable_) {
262         return;
263     }
264     setFrameParamFunc_(requestId, load, schedFrameNum, value);
265 }
266 
EnableSelfRender()267 void FrameReport::EnableSelfRender()
268 {
269     if (!enable_) {
270         return;
271     }
272     enableSelfRenderFunc_();
273 }
274 
DisableSelfRender()275 void FrameReport::DisableSelfRender()
276 {
277     if (!enable_) {
278         return;
279     }
280     disableSelfRenderFunc_();
281 }
282 } // namespace OHOS::Ace
283