1 // /*
2 //  * Copyright (c) 2023 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 "display_manager_adapter_lite.h"
17 
18 #include <iremote_broker.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21 
22 #include "display_manager_lite.h"
23 #include "screen_manager_lite.h"
24 #include "dm_common.h"
25 #include "window_manager_hilog.h"
26 
27 namespace OHOS::Rosen {
28 namespace {
29 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerAdapterLite"};
30 }
31 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerAdapterLite)
WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManagerAdapterLite)32 WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManagerAdapterLite)
33 
34 
35 #define INIT_PROXY_CHECK_RETURN(ret) \
36     do { \
37         if (!InitDMSProxy()) { \
38             WLOGFE("InitDMSProxy fail"); \
39             return ret; \
40         } \
41     } while (false)
42 
43 DMError BaseAdapterLite::RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
44     DisplayManagerAgentType type)
45 {
46     INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
47 
48     return displayManagerServiceProxy_->RegisterDisplayManagerAgent(displayManagerAgent, type);
49 }
50 
UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent> & displayManagerAgent,DisplayManagerAgentType type)51 DMError BaseAdapterLite::UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
52     DisplayManagerAgentType type)
53 {
54     INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
55 
56     return displayManagerServiceProxy_->UnregisterDisplayManagerAgent(displayManagerAgent, type);
57 }
58 
InitDMSProxy()59 bool BaseAdapterLite::InitDMSProxy()
60 {
61     std::lock_guard<std::recursive_mutex> lock(mutex_);
62     if (!isProxyValid_) {
63         sptr<ISystemAbilityManager> systemAbilityManager =
64                 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65         if (!systemAbilityManager) {
66             WLOGFE("Failed to get system ability mgr.");
67             return false;
68         }
69 
70         sptr<IRemoteObject> remoteObject
71             = systemAbilityManager->GetSystemAbility(DISPLAY_MANAGER_SERVICE_SA_ID);
72         if (!remoteObject) {
73             WLOGFE("Failed to get display manager service.");
74             return false;
75         }
76         displayManagerServiceProxy_ = new(std::nothrow) DisplayManagerLiteProxy(remoteObject);
77         if ((!displayManagerServiceProxy_) || (!displayManagerServiceProxy_->AsObject())) {
78             WLOGFW("Failed to get system display manager services");
79             return false;
80         }
81 
82         dmsDeath_ = new(std::nothrow) DMSDeathRecipientLite(*this);
83         if (dmsDeath_ == nullptr) {
84             WLOGFE("Failed to create death Recipient ptr DMSDeathRecipient");
85             return false;
86         }
87         if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(dmsDeath_)) {
88             WLOGFE("Failed to add death recipient");
89             return false;
90         }
91         isProxyValid_ = true;
92     }
93     return true;
94 }
95 
GetDefaultDisplayInfo()96 sptr<DisplayInfo> DisplayManagerAdapterLite::GetDefaultDisplayInfo()
97 {
98     INIT_PROXY_CHECK_RETURN(nullptr);
99 
100     return displayManagerServiceProxy_->GetDefaultDisplayInfo();
101 }
102 
IsFoldable()103 bool DisplayManagerAdapterLite::IsFoldable()
104 {
105     INIT_PROXY_CHECK_RETURN(false);
106 
107     return displayManagerServiceProxy_->IsFoldable();
108 }
109 
GetFoldStatus()110 FoldStatus DisplayManagerAdapterLite::GetFoldStatus()
111 {
112     INIT_PROXY_CHECK_RETURN(FoldStatus::UNKNOWN);
113 
114     return displayManagerServiceProxy_->GetFoldStatus();
115 }
116 
GetFoldDisplayMode()117 FoldDisplayMode DisplayManagerAdapterLite::GetFoldDisplayMode()
118 {
119     INIT_PROXY_CHECK_RETURN(FoldDisplayMode::UNKNOWN);
120 
121     return displayManagerServiceProxy_->GetFoldDisplayMode();
122 }
123 
SetFoldDisplayMode(const FoldDisplayMode mode)124 void DisplayManagerAdapterLite::SetFoldDisplayMode(const FoldDisplayMode mode)
125 {
126     INIT_PROXY_CHECK_RETURN();
127 
128     return displayManagerServiceProxy_->SetFoldDisplayMode(mode);
129 }
130 
GetDisplayInfo(DisplayId displayId)131 sptr<DisplayInfo> DisplayManagerAdapterLite::GetDisplayInfo(DisplayId displayId)
132 {
133     if (displayId == DISPLAY_ID_INVALID) {
134         WLOGFW("screen id is invalid");
135         return nullptr;
136     }
137     INIT_PROXY_CHECK_RETURN(nullptr);
138 
139     return displayManagerServiceProxy_->GetDisplayInfoById(displayId);
140 }
141 
GetCutoutInfo(DisplayId displayId)142 sptr<CutoutInfo> DisplayManagerAdapterLite::GetCutoutInfo(DisplayId displayId)
143 {
144     if (displayId == DISPLAY_ID_INVALID) {
145         WLOGFE("screen id is invalid");
146         return nullptr;
147     }
148     INIT_PROXY_CHECK_RETURN(nullptr);
149     return displayManagerServiceProxy_->GetCutoutInfo(displayId);
150 }
151 
152 /*
153  * used by powermgr
154  */
WakeUpBegin(PowerStateChangeReason reason)155 bool DisplayManagerAdapterLite::WakeUpBegin(PowerStateChangeReason reason)
156 {
157     INIT_PROXY_CHECK_RETURN(false);
158 
159     return displayManagerServiceProxy_->WakeUpBegin(reason);
160 }
161 
WakeUpEnd()162 bool DisplayManagerAdapterLite::WakeUpEnd()
163 {
164     INIT_PROXY_CHECK_RETURN(false);
165 
166     return displayManagerServiceProxy_->WakeUpEnd();
167 }
168 
SuspendBegin(PowerStateChangeReason reason)169 bool DisplayManagerAdapterLite::SuspendBegin(PowerStateChangeReason reason)
170 {
171     INIT_PROXY_CHECK_RETURN(false);
172 
173     return displayManagerServiceProxy_->SuspendBegin(reason);
174 }
175 
SuspendEnd()176 bool DisplayManagerAdapterLite::SuspendEnd()
177 {
178     INIT_PROXY_CHECK_RETURN(false);
179 
180     return displayManagerServiceProxy_->SuspendEnd();
181 }
182 
SetDisplayState(DisplayState state)183 bool DisplayManagerAdapterLite::SetDisplayState(DisplayState state)
184 {
185     INIT_PROXY_CHECK_RETURN(false);
186 
187     return displayManagerServiceProxy_->SetDisplayState(state);
188 }
189 
GetDisplayState(DisplayId displayId)190 DisplayState DisplayManagerAdapterLite::GetDisplayState(DisplayId displayId)
191 {
192     INIT_PROXY_CHECK_RETURN(DisplayState::UNKNOWN);
193 
194     return displayManagerServiceProxy_->GetDisplayState(displayId);
195 }
196 
TryToCancelScreenOff()197 bool DisplayManagerAdapterLite::TryToCancelScreenOff()
198 {
199     INIT_PROXY_CHECK_RETURN(false);
200 
201     return displayManagerServiceProxy_->TryToCancelScreenOff();
202 }
203 
SetScreenBrightness(uint64_t screenId,uint32_t level)204 bool DisplayManagerAdapterLite::SetScreenBrightness(uint64_t screenId, uint32_t level)
205 {
206     INIT_PROXY_CHECK_RETURN(false);
207 
208     return displayManagerServiceProxy_->SetScreenBrightness(screenId, level);
209 }
210 
GetScreenBrightness(uint64_t screenId)211 uint32_t DisplayManagerAdapterLite::GetScreenBrightness(uint64_t screenId)
212 {
213     INIT_PROXY_CHECK_RETURN(false);
214 
215     return displayManagerServiceProxy_->GetScreenBrightness(screenId);
216 }
217 
GetAllDisplayIds()218 std::vector<DisplayId> DisplayManagerAdapterLite::GetAllDisplayIds()
219 {
220     WLOGFD("DisplayManagerAdapterLite::GetAllDisplayIds enter");
221     INIT_PROXY_CHECK_RETURN(std::vector<DisplayId>());
222 
223     return displayManagerServiceProxy_->GetAllDisplayIds();
224 }
225 
GetVirtualScreenFlag(ScreenId screenId)226 VirtualScreenFlag DisplayManagerAdapterLite::GetVirtualScreenFlag(ScreenId screenId)
227 {
228     INIT_PROXY_CHECK_RETURN(VirtualScreenFlag::DEFAULT);
229     if (screenId == SCREEN_ID_INVALID) {
230         WLOGFE("screenId id is invalid");
231         return VirtualScreenFlag::DEFAULT;
232     }
233     return displayManagerServiceProxy_->GetVirtualScreenFlag(screenId);
234 }
235 
SetSpecifiedScreenPower(ScreenId screenId,ScreenPowerState state,PowerStateChangeReason reason)236 bool ScreenManagerAdapterLite::SetSpecifiedScreenPower(ScreenId screenId, ScreenPowerState state,
237     PowerStateChangeReason reason)
238 {
239     INIT_PROXY_CHECK_RETURN(false);
240 
241     return displayManagerServiceProxy_->SetSpecifiedScreenPower(screenId, state, reason);
242 }
243 
SetScreenPowerForAll(ScreenPowerState state,PowerStateChangeReason reason)244 bool ScreenManagerAdapterLite::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)
245 {
246     INIT_PROXY_CHECK_RETURN(false);
247 
248     return displayManagerServiceProxy_->SetScreenPowerForAll(state, reason);
249 }
250 
GetScreenPower(ScreenId dmsScreenId)251 ScreenPowerState ScreenManagerAdapterLite::GetScreenPower(ScreenId dmsScreenId)
252 {
253     INIT_PROXY_CHECK_RETURN(ScreenPowerState::INVALID_STATE);
254 
255     return displayManagerServiceProxy_->GetScreenPower(dmsScreenId);
256 }
257 
DMSDeathRecipientLite(BaseAdapterLite & adapter)258 DMSDeathRecipientLite::DMSDeathRecipientLite(BaseAdapterLite& adapter) : adapter_(adapter)
259 {
260 }
261 
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)262 void DMSDeathRecipientLite::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
263 {
264     if (wptrDeath == nullptr) {
265         WLOGFE("wptrDeath is nullptr");
266         return;
267     }
268 
269     sptr<IRemoteObject> object = wptrDeath.promote();
270     if (!object) {
271         WLOGFE("object is nullptr");
272         return;
273     }
274     WLOGFI("dms OnRemoteDied");
275     adapter_.Clear();
276     if (SingletonContainer::IsDestroyed()) {
277         WLOGFE("SingletonContainer is destroyed");
278         return;
279     }
280     SingletonContainer::Get<DisplayManagerLite>().OnRemoteDied();
281     SingletonContainer::Get<ScreenManagerLite>().OnRemoteDied();
282     return;
283 }
284 
~BaseAdapterLite()285 BaseAdapterLite::~BaseAdapterLite()
286 {
287     WLOGFI("BaseAdapterLite destroy");
288     std::lock_guard<std::recursive_mutex> lock(mutex_);
289     Clear();
290     displayManagerServiceProxy_ = nullptr;
291 }
292 
Clear()293 void BaseAdapterLite::Clear()
294 {
295     WLOGFI("BaseAdapterLite Clear");
296     std::lock_guard<std::recursive_mutex> lock(mutex_);
297     if ((displayManagerServiceProxy_ != nullptr) && (displayManagerServiceProxy_->AsObject() != nullptr)) {
298         displayManagerServiceProxy_->AsObject()->RemoveDeathRecipient(dmsDeath_);
299     }
300     isProxyValid_ = false;
301 }
302 } // namespace OHOS::Rosen