1 /*
2 * Copyright (c) 2021-2022 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.h"
17
18 #include <iremote_broker.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21
22 #include "display_manager.h"
23 #include "screen_manager.h"
24 #include "window_manager_hilog.h"
25
26 namespace OHOS::Rosen {
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerAdapter"};
29 }
30 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerAdapter)
WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManagerAdapter)31 WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManagerAdapter)
32
33 #define INIT_PROXY_CHECK_RETURN(ret) \
34 do { \
35 if (!InitDMSProxy()) { \
36 WLOGFE("InitDMSProxy failed!"); \
37 return ret; \
38 } \
39 } while (false)
40
41 sptr<DisplayInfo> DisplayManagerAdapter::GetDefaultDisplayInfo()
42 {
43 INIT_PROXY_CHECK_RETURN(nullptr);
44
45 return displayManagerServiceProxy_->GetDefaultDisplayInfo();
46 }
47
GetDisplayInfoByScreenId(ScreenId screenId)48 sptr<DisplayInfo> DisplayManagerAdapter::GetDisplayInfoByScreenId(ScreenId screenId)
49 {
50 INIT_PROXY_CHECK_RETURN(nullptr);
51
52 return displayManagerServiceProxy_->GetDisplayInfoByScreen(screenId);
53 }
54
GetDisplaySnapshot(DisplayId displayId)55 std::shared_ptr<Media::PixelMap> DisplayManagerAdapter::GetDisplaySnapshot(DisplayId displayId)
56 {
57 INIT_PROXY_CHECK_RETURN(nullptr);
58
59 return displayManagerServiceProxy_->GetDisplaySnapshot(displayId);
60 }
61
GetScreenSupportedColorGamuts(ScreenId screenId,std::vector<ScreenColorGamut> & colorGamuts)62 DMError ScreenManagerAdapter::GetScreenSupportedColorGamuts(ScreenId screenId,
63 std::vector<ScreenColorGamut>& colorGamuts)
64 {
65 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
66
67 return displayManagerServiceProxy_->GetScreenSupportedColorGamuts(screenId, colorGamuts);
68 }
69
GetScreenColorGamut(ScreenId screenId,ScreenColorGamut & colorGamut)70 DMError ScreenManagerAdapter::GetScreenColorGamut(ScreenId screenId, ScreenColorGamut& colorGamut)
71 {
72 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
73
74 return displayManagerServiceProxy_->GetScreenColorGamut(screenId, colorGamut);
75 }
76
SetScreenColorGamut(ScreenId screenId,int32_t colorGamutIdx)77 DMError ScreenManagerAdapter::SetScreenColorGamut(ScreenId screenId, int32_t colorGamutIdx)
78 {
79 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
80
81 return displayManagerServiceProxy_->SetScreenColorGamut(screenId, colorGamutIdx);
82 }
83
GetScreenGamutMap(ScreenId screenId,ScreenGamutMap & gamutMap)84 DMError ScreenManagerAdapter::GetScreenGamutMap(ScreenId screenId, ScreenGamutMap& gamutMap)
85 {
86 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
87
88 return displayManagerServiceProxy_->GetScreenGamutMap(screenId, gamutMap);
89 }
90
SetScreenGamutMap(ScreenId screenId,ScreenGamutMap gamutMap)91 DMError ScreenManagerAdapter::SetScreenGamutMap(ScreenId screenId, ScreenGamutMap gamutMap)
92 {
93 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
94
95 return displayManagerServiceProxy_->SetScreenGamutMap(screenId, gamutMap);
96 }
97
SetScreenColorTransform(ScreenId screenId)98 DMError ScreenManagerAdapter::SetScreenColorTransform(ScreenId screenId)
99 {
100 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
101
102 return displayManagerServiceProxy_->SetScreenColorTransform(screenId);
103 }
104
CreateVirtualScreen(VirtualScreenOption option,const sptr<IDisplayManagerAgent> & displayManagerAgent)105 ScreenId ScreenManagerAdapter::CreateVirtualScreen(VirtualScreenOption option,
106 const sptr<IDisplayManagerAgent>& displayManagerAgent)
107 {
108 INIT_PROXY_CHECK_RETURN(SCREEN_ID_INVALID);
109
110 WLOGFI("DisplayManagerAdapter::CreateVirtualScreen");
111 return displayManagerServiceProxy_->CreateVirtualScreen(option, displayManagerAgent->AsObject());
112 }
113
DestroyVirtualScreen(ScreenId screenId)114 DMError ScreenManagerAdapter::DestroyVirtualScreen(ScreenId screenId)
115 {
116 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
117
118 WLOGFI("DisplayManagerAdapter::DestroyVirtualScreen");
119 return displayManagerServiceProxy_->DestroyVirtualScreen(screenId);
120 }
121
SetVirtualScreenSurface(ScreenId screenId,sptr<Surface> surface)122 DMError ScreenManagerAdapter::SetVirtualScreenSurface(ScreenId screenId, sptr<Surface> surface)
123 {
124 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
125
126 WLOGFI("DisplayManagerAdapter::SetVirtualScreenSurface");
127 return displayManagerServiceProxy_->SetVirtualScreenSurface(screenId, surface);
128 }
129
SetScreenRotationLocked(bool isLocked)130 void ScreenManagerAdapter::SetScreenRotationLocked(bool isLocked)
131 {
132 INIT_PROXY_CHECK_RETURN();
133 WLOGFI("DisplayManagerAdapter::SetScreenRotationLocked");
134 displayManagerServiceProxy_->SetScreenRotationLocked(isLocked);
135 }
136
IsScreenRotationLocked()137 bool ScreenManagerAdapter::IsScreenRotationLocked()
138 {
139 INIT_PROXY_CHECK_RETURN(false);
140 WLOGFI("DisplayManagerAdapter::IsScreenRotationLocked");
141 return displayManagerServiceProxy_->IsScreenRotationLocked();
142 }
143
SetScreenPowerForAll(ScreenPowerState state,PowerStateChangeReason reason)144 bool ScreenManagerAdapter::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)
145 {
146 INIT_PROXY_CHECK_RETURN(false);
147 return displayManagerServiceProxy_->SetScreenPowerForAll(state, reason);
148 }
149
GetScreenPower(ScreenId dmsScreenId)150 ScreenPowerState ScreenManagerAdapter::GetScreenPower(ScreenId dmsScreenId)
151 {
152 INIT_PROXY_CHECK_RETURN(ScreenPowerState::INVALID_STATE);
153 return displayManagerServiceProxy_->GetScreenPower(dmsScreenId);
154 }
155
SetOrientation(ScreenId screenId,Orientation orientation)156 bool ScreenManagerAdapter::SetOrientation(ScreenId screenId, Orientation orientation)
157 {
158 INIT_PROXY_CHECK_RETURN(false);
159
160 return displayManagerServiceProxy_->SetOrientation(screenId, orientation);
161 }
162
RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent> & displayManagerAgent,DisplayManagerAgentType type)163 bool BaseAdapter::RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
164 DisplayManagerAgentType type)
165 {
166 INIT_PROXY_CHECK_RETURN(false);
167
168 return displayManagerServiceProxy_->RegisterDisplayManagerAgent(displayManagerAgent, type);
169 }
170
UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent> & displayManagerAgent,DisplayManagerAgentType type)171 bool BaseAdapter::UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
172 DisplayManagerAgentType type)
173 {
174 INIT_PROXY_CHECK_RETURN(false);
175
176 return displayManagerServiceProxy_->UnregisterDisplayManagerAgent(displayManagerAgent, type);
177 }
178
WakeUpBegin(PowerStateChangeReason reason)179 bool DisplayManagerAdapter::WakeUpBegin(PowerStateChangeReason reason)
180 {
181 INIT_PROXY_CHECK_RETURN(false);
182
183 return displayManagerServiceProxy_->WakeUpBegin(reason);
184 }
185
WakeUpEnd()186 bool DisplayManagerAdapter::WakeUpEnd()
187 {
188 INIT_PROXY_CHECK_RETURN(false);
189
190 return displayManagerServiceProxy_->WakeUpEnd();
191 }
192
SuspendBegin(PowerStateChangeReason reason)193 bool DisplayManagerAdapter::SuspendBegin(PowerStateChangeReason reason)
194 {
195 INIT_PROXY_CHECK_RETURN(false);
196
197 return displayManagerServiceProxy_->SuspendBegin(reason);
198 }
199
SuspendEnd()200 bool DisplayManagerAdapter::SuspendEnd()
201 {
202 INIT_PROXY_CHECK_RETURN(false);
203
204 return displayManagerServiceProxy_->SuspendEnd();
205 }
206
SetDisplayState(DisplayState state)207 bool DisplayManagerAdapter::SetDisplayState(DisplayState state)
208 {
209 INIT_PROXY_CHECK_RETURN(false);
210
211 return displayManagerServiceProxy_->SetDisplayState(state);
212 }
213
GetDisplayState(DisplayId displayId)214 DisplayState DisplayManagerAdapter::GetDisplayState(DisplayId displayId)
215 {
216 INIT_PROXY_CHECK_RETURN(DisplayState::UNKNOWN);
217
218 return displayManagerServiceProxy_->GetDisplayState(displayId);
219 }
220
NotifyDisplayEvent(DisplayEvent event)221 void DisplayManagerAdapter::NotifyDisplayEvent(DisplayEvent event)
222 {
223 INIT_PROXY_CHECK_RETURN();
224
225 displayManagerServiceProxy_->NotifyDisplayEvent(event);
226 }
227
SetFreeze(std::vector<DisplayId> displayIds,bool isFreeze)228 bool DisplayManagerAdapter::SetFreeze(std::vector<DisplayId> displayIds, bool isFreeze)
229 {
230 INIT_PROXY_CHECK_RETURN(false);
231 return displayManagerServiceProxy_->SetFreeze(displayIds, isFreeze);
232 }
233
InitDMSProxy()234 bool BaseAdapter::InitDMSProxy()
235 {
236 std::lock_guard<std::recursive_mutex> lock(mutex_);
237 if (!isProxyValid_) {
238 sptr<ISystemAbilityManager> systemAbilityManager =
239 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
240 if (!systemAbilityManager) {
241 WLOGFE("Failed to get system ability mgr.");
242 return false;
243 }
244
245 sptr<IRemoteObject> remoteObject
246 = systemAbilityManager->GetSystemAbility(DISPLAY_MANAGER_SERVICE_SA_ID);
247 if (!remoteObject) {
248 WLOGFE("Failed to get display manager service.");
249 return false;
250 }
251
252 displayManagerServiceProxy_ = iface_cast<IDisplayManager>(remoteObject);
253 if ((!displayManagerServiceProxy_) || (!displayManagerServiceProxy_->AsObject())) {
254 WLOGFE("Failed to get system display manager services");
255 return false;
256 }
257
258 dmsDeath_ = new(std::nothrow) DMSDeathRecipient(*this);
259 if (dmsDeath_ == nullptr) {
260 WLOGFE("Failed to create death Recipient ptr DMSDeathRecipient");
261 return false;
262 }
263 if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(dmsDeath_)) {
264 WLOGFE("Failed to add death recipient");
265 return false;
266 }
267 isProxyValid_ = true;
268 }
269 return true;
270 }
271
DMSDeathRecipient(BaseAdapter & adapter)272 DMSDeathRecipient::DMSDeathRecipient(BaseAdapter& adapter) : adapter_(adapter)
273 {
274 }
275
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)276 void DMSDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
277 {
278 if (wptrDeath == nullptr) {
279 WLOGFE("wptrDeath is null");
280 return;
281 }
282
283 sptr<IRemoteObject> object = wptrDeath.promote();
284 if (!object) {
285 WLOGFE("object is null");
286 return;
287 }
288 WLOGFI("dms OnRemoteDied");
289 adapter_.Clear();
290 SingletonContainer::Get<DisplayManager>().OnRemoteDied();
291 SingletonContainer::Get<ScreenManager>().OnRemoteDied();
292 return;
293 }
294
Clear()295 void BaseAdapter::Clear()
296 {
297 if ((displayManagerServiceProxy_ != nullptr) && (displayManagerServiceProxy_->AsObject() != nullptr)) {
298 displayManagerServiceProxy_->AsObject()->RemoveDeathRecipient(dmsDeath_);
299 }
300 std::lock_guard<std::recursive_mutex> lock(mutex_);
301 isProxyValid_ = false;
302 }
303
MakeMirror(ScreenId mainScreenId,std::vector<ScreenId> mirrorScreenId)304 ScreenId ScreenManagerAdapter::MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenId)
305 {
306 INIT_PROXY_CHECK_RETURN(SCREEN_ID_INVALID);
307
308 return displayManagerServiceProxy_->MakeMirror(mainScreenId, mirrorScreenId);
309 }
310
GetScreenInfo(ScreenId screenId)311 sptr<ScreenInfo> ScreenManagerAdapter::GetScreenInfo(ScreenId screenId)
312 {
313 if (screenId == SCREEN_ID_INVALID) {
314 WLOGFE("screen id is invalid");
315 return nullptr;
316 }
317 INIT_PROXY_CHECK_RETURN(nullptr);
318
319 sptr<ScreenInfo> screenInfo = displayManagerServiceProxy_->GetScreenInfoById(screenId);
320 return screenInfo;
321 }
322
GetAllDisplayIds()323 std::vector<DisplayId> DisplayManagerAdapter::GetAllDisplayIds()
324 {
325 INIT_PROXY_CHECK_RETURN(std::vector<DisplayId>());
326
327 return displayManagerServiceProxy_->GetAllDisplayIds();
328 }
329
HasPrivateWindow(DisplayId displayId,bool & hasPrivateWindow)330 DMError DisplayManagerAdapter::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
331 {
332 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
333
334 return displayManagerServiceProxy_->HasPrivateWindow(displayId, hasPrivateWindow);
335 }
336
GetDisplayInfo(DisplayId displayId)337 sptr<DisplayInfo> DisplayManagerAdapter::GetDisplayInfo(DisplayId displayId)
338 {
339 if (displayId == DISPLAY_ID_INVALID) {
340 WLOGFE("screen id is invalid");
341 return nullptr;
342 }
343 INIT_PROXY_CHECK_RETURN(nullptr);
344
345 return displayManagerServiceProxy_->GetDisplayInfoById(displayId);
346 }
347
GetCutoutInfo(DisplayId displayId)348 sptr<CutoutInfo> DisplayManagerAdapter::GetCutoutInfo(DisplayId displayId)
349 {
350 WLOGFI("DisplayManagerAdapter::GetCutoutInfo");
351 if (displayId == DISPLAY_ID_INVALID) {
352 WLOGFE("screen id is invalid");
353 return nullptr;
354 }
355 INIT_PROXY_CHECK_RETURN(nullptr);
356 return displayManagerServiceProxy_->GetCutoutInfo(displayId);
357 }
358
GetScreenGroupInfoById(ScreenId screenId)359 sptr<ScreenGroupInfo> ScreenManagerAdapter::GetScreenGroupInfoById(ScreenId screenId)
360 {
361 if (screenId == SCREEN_ID_INVALID) {
362 WLOGFE("screenGroup id is invalid");
363 return nullptr;
364 }
365 INIT_PROXY_CHECK_RETURN(nullptr);
366
367 return displayManagerServiceProxy_->GetScreenGroupInfoById(screenId);
368 }
369
GetAllScreenInfos()370 std::vector<sptr<ScreenInfo>> ScreenManagerAdapter::GetAllScreenInfos()
371 {
372 INIT_PROXY_CHECK_RETURN(std::vector<sptr<ScreenInfo>>());
373
374 return displayManagerServiceProxy_->GetAllScreenInfos();
375 }
376
MakeExpand(std::vector<ScreenId> screenId,std::vector<Point> startPoint)377 ScreenId ScreenManagerAdapter::MakeExpand(std::vector<ScreenId> screenId, std::vector<Point> startPoint)
378 {
379 INIT_PROXY_CHECK_RETURN(SCREEN_ID_INVALID);
380
381 return displayManagerServiceProxy_->MakeExpand(screenId, startPoint);
382 }
383
RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)384 void ScreenManagerAdapter::RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)
385 {
386 INIT_PROXY_CHECK_RETURN();
387
388 displayManagerServiceProxy_->RemoveVirtualScreenFromGroup(screens);
389 }
390
SetScreenActiveMode(ScreenId screenId,uint32_t modeId)391 bool ScreenManagerAdapter::SetScreenActiveMode(ScreenId screenId, uint32_t modeId)
392 {
393 INIT_PROXY_CHECK_RETURN(false);
394
395 return displayManagerServiceProxy_->SetScreenActiveMode(screenId, modeId);
396 }
397
SetVirtualPixelRatio(ScreenId screenId,float virtualPixelRatio)398 bool ScreenManagerAdapter::SetVirtualPixelRatio(ScreenId screenId, float virtualPixelRatio)
399 {
400 INIT_PROXY_CHECK_RETURN(false);
401
402 return displayManagerServiceProxy_->SetVirtualPixelRatio(screenId, virtualPixelRatio);
403 }
404 } // namespace OHOS::Rosen