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
16 #ifdef _WIN32
17 #include <windows.h>
18 #else
19 #include <dlfcn.h>
20 #endif
21 #include "monitor/aps_monitor_impl.h"
22 #include "platform/common/rs_log.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 const std::string APS_CLIENT_SO = "libaps_client.z.so";
28 const std::string SURFACE_BOUND_CHANGE = "SURFACE_BOUND_CHANGE";
29 const std::string SURFACE_DESTROYED = "SURFACE_DESTROYED";
30 const std::string SURFACE_NODE_TREE_CHANGE = "SURFACE_NODE_TREE_CHANGE";
31 }
32
~ApsMonitorImpl()33 ApsMonitorImpl::~ApsMonitorImpl()
34 {
35 if (loadFileHandle_ != nullptr) {
36 #ifdef _WIN32
37 FreeLibrary((HMODULE)loadFileHandle_);
38 #else
39 dlclose(loadFileHandle_);
40 #endif
41 loadFileHandle_ = nullptr;
42 }
43 }
44
GetInstance()45 ApsMonitorImpl& ApsMonitorImpl::GetInstance()
46 {
47 static ApsMonitorImpl apsMonitorImpl;
48 return apsMonitorImpl;
49 }
50
SetApsSurfaceBoundChange(std::string height,std::string width,std::string id)51 void ApsMonitorImpl::SetApsSurfaceBoundChange(std::string height, std::string width, std::string id)
52 {
53 LoadApsFuncsOnce();
54 if (sendApsEventFunc_ == nullptr) {
55 return;
56 }
57 std::unordered_map<std::string, std::string> eventData;
58 eventData.emplace("height", std::move(height));
59 eventData.emplace("width", std::move(width));
60 eventData.emplace("id", std::move(id));
61 sendApsEventFunc_(SURFACE_BOUND_CHANGE, std::move(eventData));
62 }
63
SetApsSurfaceDestroyedInfo(std::string id)64 void ApsMonitorImpl::SetApsSurfaceDestroyedInfo(std::string id)
65 {
66 LoadApsFuncsOnce();
67 if (sendApsEventFunc_ == nullptr) {
68 return;
69 }
70 std::unordered_map<std::string, std::string> eventData;
71 eventData.emplace("id", std::move(id));
72 sendApsEventFunc_(SURFACE_DESTROYED, std::move(eventData));
73 }
74
SetApsSurfaceNodeTreeChange(bool onTree,std::string name)75 void ApsMonitorImpl::SetApsSurfaceNodeTreeChange(bool onTree, std::string name)
76 {
77 LoadApsFuncsOnce();
78 if (sendApsEventFunc_ == nullptr) {
79 return;
80 }
81 std::unordered_map<std::string, std::string> eventData;
82 eventData.emplace("onTree", std::to_string(onTree));
83 eventData.emplace("name", std::move(name));
84 sendApsEventFunc_(SURFACE_NODE_TREE_CHANGE, std::move(eventData));
85 }
86
LoadApsFuncsOnce()87 void ApsMonitorImpl::LoadApsFuncsOnce()
88 {
89 if (!isApsFuncsAvailable_ || isApsFuncsLoad_) {
90 return;
91 }
92 #ifdef _WIN32
93 loadFileHandle_ = (void*)LoadLibraryA(APS_CLIENT_SO.c_str());
94 #else
95 loadFileHandle_ = dlopen(APS_CLIENT_SO.c_str(), RTLD_NOW);
96 #endif
97 if (loadFileHandle_ == nullptr) {
98 isApsFuncsAvailable_ = false;
99 return;
100 }
101 #ifdef _WIN32
102 sendApsEventFunc_ = reinterpret_cast<SendApsEventFunc>(
103 (void*)GetProcAddress((HMODULE)loadFileHandle_, "SendApsEvent"));
104 #else
105 sendApsEventFunc_ = reinterpret_cast<SendApsEventFunc>(dlsym(loadFileHandle_, "SendApsEvent"));
106 #endif
107 if (sendApsEventFunc_ == nullptr) {
108 #ifdef _WIN32
109 FreeLibrary((HMODULE)loadFileHandle_);
110 #else
111 dlclose(loadFileHandle_);
112 #endif
113 isApsFuncsAvailable_ = false;
114 return;
115 }
116 isApsFuncsLoad_ = true;
117 }
118 } // namespace Rosen
119 } // namespace OHOS