• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include "frame_report_sched.h"
17 
18 #include <dlfcn.h>
19 #include <unistd.h>
20 
21 #include "event_logger.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 DEFINE_EH_HILOG_LABEL("FrameReportSched");
27 const std::string FRAME_AWARE_SO_PATH = "libframe_ui_intf.z.so";
28 constexpr int RS_UID = 1003;
29 } // namespace
GetInstance()30 FrameReport& FrameReport::GetInstance()
31 {
32     static FrameReport instance;
33     return instance;
34 }
35 
FrameReport()36 FrameReport::FrameReport()
37 {
38     LoadLibrary();
39     uid_ = getuid();
40 }
41 
~FrameReport()42 FrameReport::~FrameReport()
43 {
44     CloseLibrary();
45 }
46 
LoadLibrary()47 void FrameReport::LoadLibrary()
48 {
49     if (!frameSchedSoLoaded_) {
50         frameSchedHandle_ = dlopen(FRAME_AWARE_SO_PATH.c_str(), RTLD_LAZY);
51         if (frameSchedHandle_ == nullptr) {
52             HILOGE("[LoadLibrary] dlopen libframe_ui_intf.so failed!"
53                 " error = %{public}s\n", dlerror());
54             return;
55         }
56         frameSchedSoLoaded_ = true;
57     }
58     HILOGD("[LoadLibrary] dlopen libframe_ui_intf.so success");
59     reportSchedEventFunc_ = (ReportSchedEventFunc)LoadSymbol("ReportSchedEvent");
60     return;
61 }
62 
CloseLibrary()63 void FrameReport::CloseLibrary()
64 {
65     if (dlclose(frameSchedHandle_) != 0) {
66         HILOGE("[CloseLibrary] libframe_ui_intf.so failed!\n");
67         return;
68     }
69     frameSchedHandle_ = nullptr;
70     frameSchedSoLoaded_ = false;
71 }
72 
LoadSymbol(const char * symName)73 void* FrameReport::LoadSymbol(const char* symName)
74 {
75     if (frameSchedHandle_ == nullptr) {
76         HILOGE("[loadSymbol]libframe_ui_intf.so not loaded.\n");
77         return nullptr;
78     }
79     void *funcSym = dlsym(frameSchedHandle_, symName);
80     if (funcSym == nullptr) {
81         HILOGE("[loadSymbol] Get %{public}s symbol failed: %{public}s\n", symName, dlerror());
82         return nullptr;
83     }
84     return funcSym;
85 }
86 
ReportSchedEvent(FrameSchedEvent event,const std::unordered_map<std::string,std::string> & payload)87 void FrameReport::ReportSchedEvent(FrameSchedEvent event, const std::unordered_map<std::string, std::string> &payload)
88 {
89     if (!frameSchedSoLoaded_) {
90         HILOGD("[ReportSchedEvent] libframe_ui_intf.so is closed");
91         return;
92     }
93     if (reportSchedEventFunc_ == nullptr) {
94         HILOGD("[ReportSchedEvent] reportSchedEventFunc_ is nullptr");
95         return;
96     }
97     if (uid_ == RS_UID) {
98         HILOGD("[ReportSchedEvent] uid_ == RS_UID");
99         return;
100     }
101     reportSchedEventFunc_(event, payload);
102 }
103 }  // namespace AppExecFwk
104 }  // namespace OHOS