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)
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 
RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent, DisplayManagerAgentType type)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 
GetInternalScreenId()183 ScreenId DisplayManagerAdapterLite::GetInternalScreenId()
184 {
185     INIT_PROXY_CHECK_RETURN(false);
186 
187     return displayManagerServiceProxy_->GetInternalScreenId();
188 }
189 
SetScreenPowerById(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason)190 bool DisplayManagerAdapterLite::SetScreenPowerById(ScreenId screenId, ScreenPowerState state,
191     PowerStateChangeReason reason)
192 {
193     INIT_PROXY_CHECK_RETURN(false);
194 
195     return displayManagerServiceProxy_->SetScreenPowerById(screenId, state, reason);
196 }
197 
SetDisplayState(DisplayState state)198 bool DisplayManagerAdapterLite::SetDisplayState(DisplayState state)
199 {
200     INIT_PROXY_CHECK_RETURN(false);
201 
202     return displayManagerServiceProxy_->SetDisplayState(state);
203 }
204 
GetDisplayState(DisplayId displayId)205 DisplayState DisplayManagerAdapterLite::GetDisplayState(DisplayId displayId)
206 {
207     INIT_PROXY_CHECK_RETURN(DisplayState::UNKNOWN);
208 
209     return displayManagerServiceProxy_->GetDisplayState(displayId);
210 }
211 
TryToCancelScreenOff()212 bool DisplayManagerAdapterLite::TryToCancelScreenOff()
213 {
214     INIT_PROXY_CHECK_RETURN(false);
215 
216     return displayManagerServiceProxy_->TryToCancelScreenOff();
217 }
218 
SetScreenBrightness(uint64_t screenId, uint32_t level)219 bool DisplayManagerAdapterLite::SetScreenBrightness(uint64_t screenId, uint32_t level)
220 {
221     INIT_PROXY_CHECK_RETURN(false);
222 
223     return displayManagerServiceProxy_->SetScreenBrightness(screenId, level);
224 }
225 
GetScreenBrightness(uint64_t screenId)226 uint32_t DisplayManagerAdapterLite::GetScreenBrightness(uint64_t screenId)
227 {
228     INIT_PROXY_CHECK_RETURN(false);
229 
230     return displayManagerServiceProxy_->GetScreenBrightness(screenId);
231 }
232 
GetAllDisplayIds()233 std::vector<DisplayId> DisplayManagerAdapterLite::GetAllDisplayIds()
234 {
235     WLOGFD("DisplayManagerAdapterLite::GetAllDisplayIds enter");
236     INIT_PROXY_CHECK_RETURN(std::vector<DisplayId>());
237 
238     return displayManagerServiceProxy_->GetAllDisplayIds();
239 }
240 
SetSpecifiedScreenPower(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason)241 bool ScreenManagerAdapterLite::SetSpecifiedScreenPower(ScreenId screenId, ScreenPowerState state,
242     PowerStateChangeReason reason)
243 {
244     INIT_PROXY_CHECK_RETURN(false);
245 
246     return displayManagerServiceProxy_->SetSpecifiedScreenPower(screenId, state, reason);
247 }
248 
SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)249 bool ScreenManagerAdapterLite::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)
250 {
251     INIT_PROXY_CHECK_RETURN(false);
252 
253     return displayManagerServiceProxy_->SetScreenPowerForAll(state, reason);
254 }
255 
GetScreenPower(ScreenId dmsScreenId)256 ScreenPowerState ScreenManagerAdapterLite::GetScreenPower(ScreenId dmsScreenId)
257 {
258     INIT_PROXY_CHECK_RETURN(ScreenPowerState::INVALID_STATE);
259 
260     return displayManagerServiceProxy_->GetScreenPower(dmsScreenId);
261 }
262 
DMSDeathRecipientLite(BaseAdapterLite& adapter)263 DMSDeathRecipientLite::DMSDeathRecipientLite(BaseAdapterLite& adapter) : adapter_(adapter)
264 {
265 }
266 
OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)267 void DMSDeathRecipientLite::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
268 {
269     if (wptrDeath == nullptr) {
270         WLOGFE("wptrDeath is nullptr");
271         return;
272     }
273 
274     sptr<IRemoteObject> object = wptrDeath.promote();
275     if (!object) {
276         WLOGFE("object is nullptr");
277         return;
278     }
279     WLOGFI("dms OnRemoteDied");
280     adapter_.Clear();
281     SingletonContainer::Get<DisplayManagerLite>().OnRemoteDied();
282     SingletonContainer::Get<ScreenManagerLite>().OnRemoteDied();
283     return;
284 }
285 
~BaseAdapterLite()286 BaseAdapterLite::~BaseAdapterLite()
287 {
288     WLOGFI("BaseAdapterLite destroy");
289     std::lock_guard<std::recursive_mutex> lock(mutex_);
290     Clear();
291     displayManagerServiceProxy_ = nullptr;
292 }
293 
Clear()294 void BaseAdapterLite::Clear()
295 {
296     WLOGFI("BaseAdapterLite Clear");
297     std::lock_guard<std::recursive_mutex> lock(mutex_);
298     if ((displayManagerServiceProxy_ != nullptr) && (displayManagerServiceProxy_->AsObject() != nullptr)) {
299         displayManagerServiceProxy_->AsObject()->RemoveDeathRecipient(dmsDeath_);
300     }
301     isProxyValid_ = false;
302 }
303 } // namespace OHOS::Rosen