• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
GetVirtualScreenFlag(ScreenId screenId)241 VirtualScreenFlag DisplayManagerAdapterLite::GetVirtualScreenFlag(ScreenId screenId)
242 {
243     INIT_PROXY_CHECK_RETURN(VirtualScreenFlag::DEFAULT);
244     if (screenId == SCREEN_ID_INVALID) {
245         WLOGFE("screenId id is invalid");
246         return VirtualScreenFlag::DEFAULT;
247     }
248     return displayManagerServiceProxy_->GetVirtualScreenFlag(screenId);
249 }
250 
SetSystemKeyboardStatus(bool isOn)251 DMError DisplayManagerAdapterLite::SetSystemKeyboardStatus(bool isOn)
252 {
253     INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
254 
255     return displayManagerServiceProxy_->SetSystemKeyboardStatus(isOn);
256 }
257 
SetSpecifiedScreenPower(ScreenId screenId,ScreenPowerState state,PowerStateChangeReason reason)258 bool ScreenManagerAdapterLite::SetSpecifiedScreenPower(ScreenId screenId, ScreenPowerState state,
259     PowerStateChangeReason reason)
260 {
261     INIT_PROXY_CHECK_RETURN(false);
262 
263     return displayManagerServiceProxy_->SetSpecifiedScreenPower(screenId, state, reason);
264 }
265 
SetScreenPowerForAll(ScreenPowerState state,PowerStateChangeReason reason)266 bool ScreenManagerAdapterLite::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)
267 {
268     INIT_PROXY_CHECK_RETURN(false);
269 
270     return displayManagerServiceProxy_->SetScreenPowerForAll(state, reason);
271 }
272 
GetScreenPower(ScreenId dmsScreenId)273 ScreenPowerState ScreenManagerAdapterLite::GetScreenPower(ScreenId dmsScreenId)
274 {
275     INIT_PROXY_CHECK_RETURN(ScreenPowerState::INVALID_STATE);
276 
277     return displayManagerServiceProxy_->GetScreenPower(dmsScreenId);
278 }
279 
GetScreenPower()280 ScreenPowerState ScreenManagerAdapterLite::GetScreenPower()
281 {
282     INIT_PROXY_CHECK_RETURN(ScreenPowerState::INVALID_STATE);
283 
284     return displayManagerServiceProxy_->GetScreenPower();
285 }
286 
GetAllScreenInfos(std::vector<sptr<ScreenInfo>> & screenInfos)287 DMError ScreenManagerAdapterLite::GetAllScreenInfos(std::vector<sptr<ScreenInfo>>& screenInfos)
288 {
289     INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
290 
291     return displayManagerServiceProxy_->GetAllScreenInfos(screenInfos);
292 }
293 
DMSDeathRecipientLite(BaseAdapterLite & adapter)294 DMSDeathRecipientLite::DMSDeathRecipientLite(BaseAdapterLite& adapter) : adapter_(adapter)
295 {
296 }
297 
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)298 void DMSDeathRecipientLite::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
299 {
300     if (wptrDeath == nullptr) {
301         WLOGFE("wptrDeath is nullptr");
302         return;
303     }
304 
305     sptr<IRemoteObject> object = wptrDeath.promote();
306     if (!object) {
307         WLOGFE("object is nullptr");
308         return;
309     }
310     WLOGFI("dms OnRemoteDied");
311     adapter_.Clear();
312     if (SingletonContainer::IsDestroyed()) {
313         WLOGFE("SingletonContainer is destroyed");
314         return;
315     }
316     SingletonContainer::Get<DisplayManagerLite>().OnRemoteDied();
317     SingletonContainer::Get<ScreenManagerLite>().OnRemoteDied();
318     return;
319 }
320 
~BaseAdapterLite()321 BaseAdapterLite::~BaseAdapterLite()
322 {
323     WLOGFI("destroy");
324     std::lock_guard<std::recursive_mutex> lock(mutex_);
325     Clear();
326     displayManagerServiceProxy_ = nullptr;
327 }
328 
Clear()329 void BaseAdapterLite::Clear()
330 {
331     WLOGFI("Clear");
332     std::lock_guard<std::recursive_mutex> lock(mutex_);
333     if ((displayManagerServiceProxy_ != nullptr) && (displayManagerServiceProxy_->AsObject() != nullptr)) {
334         displayManagerServiceProxy_->AsObject()->RemoveDeathRecipient(dmsDeath_);
335     }
336     isProxyValid_ = false;
337 }
338 } // namespace OHOS::Rosen