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 "screen_session_manager_client.h"
17
18 #include <hitrace_meter.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21 #include <transaction/rs_transaction.h>
22 #include <transaction/rs_interfaces.h>
23 #include "dm_common.h"
24 #include "pipeline/rs_node_map.h"
25 #include "window_manager_hilog.h"
26 #include "fold_screen_controller/super_fold_state_manager.h"
27 #include "fold_screen_state_internel.h"
28 #include "rs_adapter.h"
29
30 namespace OHOS::Rosen {
31 namespace {
32 constexpr int LINE_WIDTH = 30;
33 } // namespace
34
WM_IMPLEMENT_SINGLE_INSTANCE(ScreenSessionManagerClient)35 WM_IMPLEMENT_SINGLE_INSTANCE(ScreenSessionManagerClient)
36
37 void ScreenSessionManagerClient::ConnectToServer()
38 {
39 if (screenSessionManager_) {
40 TLOGI(WmsLogTag::DMS, "Success to get screen session manager proxy");
41 return;
42 }
43 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44 if (!systemAbilityMgr) {
45 TLOGE(WmsLogTag::DMS, "Failed to get system ability mgr");
46 return;
47 }
48
49 auto remoteObject = systemAbilityMgr->GetSystemAbility(DISPLAY_MANAGER_SERVICE_SA_ID);
50 if (!remoteObject) {
51 TLOGE(WmsLogTag::DMS, "Failed to get display manager service");
52 return;
53 }
54
55 screenSessionManager_ = iface_cast<IScreenSessionManager>(remoteObject);
56 if (!screenSessionManager_) {
57 TLOGE(WmsLogTag::DMS, "Failed to get screen session manager proxy");
58 return;
59 }
60 screenSessionManager_->SetClient(this);
61 }
62
RegisterScreenConnectionListener(IScreenConnectionListener * listener)63 void ScreenSessionManagerClient::RegisterScreenConnectionListener(IScreenConnectionListener* listener)
64 {
65 if (listener == nullptr) {
66 TLOGE(WmsLogTag::DMS, "Failed to register screen connection listener, listener is null");
67 return;
68 }
69
70 screenConnectionListener_ = listener;
71 ConnectToServer();
72 TLOGI(WmsLogTag::DMS, "Success to register screen connection listener");
73 }
74
RegisterScreenConnectionChangeListener(const sptr<IScreenConnectionChangeListener> & listener)75 void ScreenSessionManagerClient::RegisterScreenConnectionChangeListener(
76 const sptr<IScreenConnectionChangeListener>& listener)
77 {
78 if (listener == nullptr) {
79 TLOGE(WmsLogTag::DMS, "Failed: listener is null");
80 return;
81 }
82 screenConnectionChangeListener_ = listener;
83 TLOGI(WmsLogTag::DMS, "Success");
84 }
85
NotifyScreenConnect(const sptr<ScreenSession> & screenSession)86 void ScreenSessionManagerClient::NotifyScreenConnect(const sptr<ScreenSession>& screenSession)
87 {
88 if (screenConnectionListener_) {
89 screenConnectionListener_->OnScreenConnected(screenSession);
90 }
91 if (screenConnectionChangeListener_) {
92 screenConnectionChangeListener_->OnScreenConnected(screenSession);
93 }
94 }
95
NotifyScreenDisconnect(const sptr<ScreenSession> & screenSession)96 void ScreenSessionManagerClient::NotifyScreenDisconnect(const sptr<ScreenSession>& screenSession)
97 {
98 if (screenConnectionListener_) {
99 screenConnectionListener_->OnScreenDisconnected(screenSession);
100 }
101 if (screenConnectionChangeListener_) {
102 screenConnectionChangeListener_->OnScreenDisconnected(screenSession);
103 }
104 }
105
CheckIfNeedConnectScreen(SessionOption option)106 bool ScreenSessionManagerClient::CheckIfNeedConnectScreen(SessionOption option)
107 {
108 if (option.rsId_ == SCREEN_ID_INVALID) {
109 TLOGE(WmsLogTag::DMS, "rsId is invalid");
110 return false;
111 }
112 if (!screenSessionManager_) {
113 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is nullptr");
114 return false;
115 }
116 if (screenSessionManager_->GetScreenProperty(option.screenId_).GetScreenType() == ScreenType::VIRTUAL) {
117 if (option.name_ == "HiCar" || option.name_ == "SuperLauncher" || option.name_ == "CastEngine" ||
118 option.name_ == "DevEcoViewer" || option.innerName_ == "CustomScbScreen" || option.name_ == "CeliaView" ||
119 option.name_ == "PadWithCar" || option.name_ == "CooperationExtend") {
120 TLOGI(WmsLogTag::DMS, "HiCar or SuperLauncher or CastEngine or DevEcoViewer or CeliaView, "
121 "need to connect the screen");
122 return true;
123 } else {
124 TLOGE(WmsLogTag::DMS, "ScreenType is virtual, no need to connect the screen");
125 return false;
126 }
127 }
128 return true;
129 }
130
OnScreenConnectionChanged(SessionOption option,ScreenEvent screenEvent)131 void ScreenSessionManagerClient::OnScreenConnectionChanged(SessionOption option, ScreenEvent screenEvent)
132 {
133 TLOGI(WmsLogTag::DMS,
134 "sId: %{public}" PRIu64 " sEvent: %{public}d rsId: %{public}" PRIu64 " name: %{public}s iName: %{public}s",
135 option.screenId_, static_cast<int>(screenEvent), option.rsId_, option.name_.c_str(), option.innerName_.c_str());
136 std::unique_lock<std::mutex> lock(screenEventMutex_);
137 auto iter = connectedScreenSet_.find(option.screenId_);
138 if (iter == connectedScreenSet_.end()) {
139 if (screenEvent == ScreenEvent::DISCONNECTED) {
140 TLOGW(WmsLogTag::DMS,
141 "discard disconnect task, sid:%{public}" PRIu64" sEvent:%{public}d rsId: %{public}" PRIu64,
142 option.screenId_, static_cast<int>(screenEvent), option.rsId_);
143 return;
144 }
145 } else {
146 if (screenEvent == ScreenEvent::CONNECTED) {
147 TLOGE(WmsLogTag::DMS,
148 "discard connect task, sid:%{public}" PRIu64" sEvent:%{public}d rsId: %{public}" PRIu64,
149 option.screenId_, static_cast<int>(screenEvent), option.rsId_);
150 return;
151 }
152 }
153 if (screenEvent == ScreenEvent::CONNECTED) {
154 if (HandleScreenConnection(option)) {
155 connectedScreenSet_.insert(option.screenId_);
156 TLOGI(WmsLogTag::DMS,
157 "screen event wait connecting, sId: %{public}" PRIu64 " sEvent: %{public}d rsId: %{public}" PRIu64,
158 option.screenId_, static_cast<int>(screenEvent), option.rsId_);
159 } else {
160 TLOGI(WmsLogTag::DMS,
161 "screen event not conent, sId: %{public}" PRIu64 " sEvent: %{public}d rsId: %{public}" PRIu64,
162 option.screenId_, static_cast<int>(screenEvent), option.rsId_);
163 }
164 }
165 if (screenEvent == ScreenEvent::DISCONNECTED) {
166 if (HandleScreenDisconnection(option)) {
167 connectedScreenSet_.erase(option.screenId_);
168 TLOGI(WmsLogTag::DMS,
169 "screen event wait disconnecting, sId: %{public}" PRIu64 " sEvent: %{public}d rsId: %{public}" PRIu64,
170 option.screenId_, static_cast<int>(screenEvent), option.rsId_);
171 } else {
172 TLOGI(WmsLogTag::DMS,
173 "screen event not disconent, sId: %{public}" PRIu64 " sEvent: %{public}d rsId: %{public}" PRIu64,
174 option.screenId_, static_cast<int>(screenEvent), option.rsId_);
175 }
176 }
177 }
178
ExtraDestroyScreen(ScreenId screenId)179 void ScreenSessionManagerClient::ExtraDestroyScreen(ScreenId screenId)
180 {
181 sptr<ScreenSession> screenSession = nullptr;
182 {
183 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
184 for (const auto& iter : extraScreenSessionMap_) {
185 sptr<ScreenSession> tempScreenSession = iter.second;
186 if (tempScreenSession != nullptr) {
187 if (tempScreenSession->GetScreenId() == screenId) {
188 screenSession = tempScreenSession;
189 break;
190 }
191 }
192 }
193 }
194 if (!screenSession) {
195 TLOGE(WmsLogTag::DMS, "extra screenSession is null");
196 return;
197 }
198 TLOGI(WmsLogTag::DMS, "ScreenId:%{public}" PRIu64 ", rsId:%{public}" PRIu64,
199 screenSession->GetScreenId(), screenSession->GetRSScreenId());
200 {
201 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
202 screenSession->DestroyScreenScene();
203 extraScreenSessionMap_.erase(screenId);
204 }
205 TLOGI(WmsLogTag::DMS, "end");
206 }
207
OnScreenExtendChanged(ScreenId mainScreenId,ScreenId extendScreenId)208 void ScreenSessionManagerClient::OnScreenExtendChanged(ScreenId mainScreenId, ScreenId extendScreenId)
209 {
210 auto screenSession = GetScreenSession(mainScreenId);
211 if (!screenSession) {
212 TLOGE(WmsLogTag::DMS, "screenSession is null");
213 return;
214 }
215 TLOGI(WmsLogTag::DMS, "mainScreenId=%{public}" PRIu64" extendScreenId=%{public}" PRIu64, mainScreenId,
216 extendScreenId);
217 screenSession->ScreenExtendChange(mainScreenId, extendScreenId);
218 }
219
GetScreenSession(ScreenId screenId) const220 sptr<ScreenSession> ScreenSessionManagerClient::GetScreenSession(ScreenId screenId) const
221 {
222 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
223 auto iter = screenSessionMap_.find(screenId);
224 if (iter == screenSessionMap_.end()) {
225 TLOGE(WmsLogTag::DMS, "Error found screen session with id: %{public}" PRIu64, screenId);
226 return nullptr;
227 }
228 return iter->second;
229 }
230
GetScreenSessionExtra(ScreenId screenId) const231 sptr<ScreenSession> ScreenSessionManagerClient::GetScreenSessionExtra(ScreenId screenId) const
232 {
233 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
234 auto iter = extraScreenSessionMap_.find(screenId);
235 if (iter == extraScreenSessionMap_.end()) {
236 TLOGE(WmsLogTag::DMS, "Error found extra screen session with id: %{public}" PRIu64, screenId);
237 return nullptr;
238 }
239 return iter->second;
240 }
241
OnPropertyChanged(ScreenId screenId,const ScreenProperty & property,ScreenPropertyChangeReason reason)242 void ScreenSessionManagerClient::OnPropertyChanged(ScreenId screenId,
243 const ScreenProperty& property, ScreenPropertyChangeReason reason)
244 {
245 auto screenSession = GetScreenSession(screenId);
246 if (!screenSession) {
247 TLOGE(WmsLogTag::DMS, "screenSession is null");
248 return;
249 }
250 screenSession->PropertyChange(property, reason);
251 }
252
OnPowerStatusChanged(DisplayPowerEvent event,EventStatus status,PowerStateChangeReason reason)253 void ScreenSessionManagerClient::OnPowerStatusChanged(DisplayPowerEvent event, EventStatus status,
254 PowerStateChangeReason reason)
255 {
256 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
257 if (screenSessionMap_.empty()) {
258 TLOGE(WmsLogTag::DMS, "[UL_POWER]screenSessionMap_ is nullptr");
259 return;
260 }
261 auto screenSession = screenSessionMap_.begin()->second;
262 if (!screenSession) {
263 TLOGE(WmsLogTag::DMS, "[UL_POWER]screenSession is null");
264 return;
265 }
266 screenSession->PowerStatusChange(event, status, reason);
267 }
268
OnSensorRotationChanged(ScreenId screenId,float sensorRotation)269 void ScreenSessionManagerClient::OnSensorRotationChanged(ScreenId screenId, float sensorRotation)
270 {
271 auto screenSession = GetScreenSession(screenId);
272 if (!screenSession) {
273 TLOGE(WmsLogTag::DMS, "screenSession is null");
274 return;
275 }
276 screenSession->SensorRotationChange(sensorRotation);
277 }
278
OnHoverStatusChanged(ScreenId screenId,int32_t hoverStatus,bool needRotate)279 void ScreenSessionManagerClient::OnHoverStatusChanged(ScreenId screenId, int32_t hoverStatus, bool needRotate)
280 {
281 auto screenSession = GetScreenSession(screenId);
282 if (!screenSession) {
283 TLOGE(WmsLogTag::DMS, "screenSession is null");
284 return;
285 }
286 screenSession->HandleHoverStatusChange(hoverStatus, needRotate);
287 }
288
OnScreenOrientationChanged(ScreenId screenId,float screenOrientation)289 void ScreenSessionManagerClient::OnScreenOrientationChanged(ScreenId screenId, float screenOrientation)
290 {
291 auto screenSession = GetScreenSession(screenId);
292 if (!screenSession) {
293 TLOGE(WmsLogTag::DMS, "screenSession is null");
294 return;
295 }
296 screenSession->ScreenOrientationChange(screenOrientation);
297 }
298
OnScreenRotationLockedChanged(ScreenId screenId,bool isLocked)299 void ScreenSessionManagerClient::OnScreenRotationLockedChanged(ScreenId screenId, bool isLocked)
300 {
301 auto screenSession = GetScreenSession(screenId);
302 if (!screenSession) {
303 TLOGE(WmsLogTag::DMS, "screenSession is null");
304 return;
305 }
306 screenSession->SetScreenRotationLocked(isLocked);
307 }
308
OnCameraBackSelfieChanged(ScreenId screenId,bool isCameraBackSelfie)309 void ScreenSessionManagerClient::OnCameraBackSelfieChanged(ScreenId screenId, bool isCameraBackSelfie)
310 {
311 auto screenSession = GetScreenSession(screenId);
312 if (!screenSession) {
313 TLOGE(WmsLogTag::DMS, "screenSession is null");
314 return;
315 }
316 screenSession->HandleCameraBackSelfieChange(isCameraBackSelfie);
317 }
318
RegisterDisplayChangeListener(const sptr<IDisplayChangeListener> & listener)319 void ScreenSessionManagerClient::RegisterDisplayChangeListener(const sptr<IDisplayChangeListener>& listener)
320 {
321 displayChangeListener_ = listener;
322 }
323
RegisterSwitchingToAnotherUserFunction(std::function<void ()> && func)324 void ScreenSessionManagerClient::RegisterSwitchingToAnotherUserFunction(std::function<void()>&& func)
325 {
326 switchingToAnotherUserFunc_ = func;
327 }
328
OnDisplayStateChanged(DisplayId defaultDisplayId,sptr<DisplayInfo> displayInfo,const std::map<DisplayId,sptr<DisplayInfo>> & displayInfoMap,DisplayStateChangeType type)329 void ScreenSessionManagerClient::OnDisplayStateChanged(DisplayId defaultDisplayId, sptr<DisplayInfo> displayInfo,
330 const std::map<DisplayId, sptr<DisplayInfo>>& displayInfoMap, DisplayStateChangeType type)
331 {
332 if (displayChangeListener_) {
333 displayChangeListener_->OnDisplayStateChange(defaultDisplayId, displayInfo, displayInfoMap, type);
334 }
335 }
336
OnUpdateFoldDisplayMode(FoldDisplayMode displayMode)337 void ScreenSessionManagerClient::OnUpdateFoldDisplayMode(FoldDisplayMode displayMode)
338 {
339 displayMode_ = displayMode;
340 }
341
OnGetSurfaceNodeIdsFromMissionIdsChanged(std::vector<uint64_t> & missionIds,std::vector<uint64_t> & surfaceNodeIds,bool isBlackList)342 void ScreenSessionManagerClient::OnGetSurfaceNodeIdsFromMissionIdsChanged(std::vector<uint64_t>& missionIds,
343 std::vector<uint64_t>& surfaceNodeIds, bool isBlackList)
344 {
345 if (displayChangeListener_) {
346 displayChangeListener_->OnGetSurfaceNodeIdsFromMissionIds(missionIds, surfaceNodeIds, isBlackList);
347 }
348 }
349
OnSetSurfaceNodeIdsChanged(DisplayId displayId,const std::vector<uint64_t> & surfaceNodeIds)350 void ScreenSessionManagerClient::OnSetSurfaceNodeIdsChanged(DisplayId displayId,
351 const std::vector<uint64_t>& surfaceNodeIds)
352 {
353 if (displayChangeListener_) {
354 displayChangeListener_->OnSetSurfaceNodeIds(displayId, surfaceNodeIds);
355 }
356 }
357
OnVirtualScreenDisconnected(DisplayId displayId)358 void ScreenSessionManagerClient::OnVirtualScreenDisconnected(DisplayId displayId)
359 {
360 if (displayChangeListener_) {
361 displayChangeListener_->OnVirtualScreenDisconnected(displayId);
362 }
363 }
364
OnScreenshot(DisplayId displayId)365 void ScreenSessionManagerClient::OnScreenshot(DisplayId displayId)
366 {
367 if (displayChangeListener_) {
368 displayChangeListener_->OnScreenshot(displayId);
369 }
370 }
371
OnImmersiveStateChanged(ScreenId screenId,bool & immersive)372 void ScreenSessionManagerClient::OnImmersiveStateChanged(ScreenId screenId, bool& immersive)
373 {
374 if (displayChangeListener_ != nullptr) {
375 displayChangeListener_->OnImmersiveStateChange(screenId, immersive);
376 }
377 }
378
GetAllScreensProperties() const379 std::map<ScreenId, ScreenProperty> ScreenSessionManagerClient::GetAllScreensProperties() const
380 {
381 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
382 std::map<ScreenId, ScreenProperty> screensProperties;
383 for (const auto& iter: screenSessionMap_) {
384 if (iter.second == nullptr) {
385 continue;
386 }
387 screensProperties[iter.first] = iter.second->GetScreenProperty();
388 }
389 return screensProperties;
390 }
391
GetFoldDisplayMode() const392 FoldDisplayMode ScreenSessionManagerClient::GetFoldDisplayMode() const
393 {
394 return displayMode_;
395 }
396
UpdateScreenRotationProperty(ScreenId screenId,const RRect & bounds,ScreenDirectionInfo directionInfo,ScreenPropertyChangeType screenPropertyChangeType)397 void ScreenSessionManagerClient::UpdateScreenRotationProperty(ScreenId screenId, const RRect& bounds,
398 ScreenDirectionInfo directionInfo, ScreenPropertyChangeType screenPropertyChangeType)
399 {
400 if (!screenSessionManager_) {
401 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
402 return;
403 }
404 screenSessionManager_->UpdateScreenDirectionInfo(screenId, directionInfo, screenPropertyChangeType, bounds);
405 screenSessionManager_->UpdateScreenRotationProperty(screenId, bounds, directionInfo.notifyRotation_,
406 screenPropertyChangeType);
407
408 // not need update property to input manager
409 if (screenPropertyChangeType == ScreenPropertyChangeType::ROTATION_END ||
410 screenPropertyChangeType == ScreenPropertyChangeType::ROTATION_UPDATE_PROPERTY_ONLY ||
411 screenPropertyChangeType == ScreenPropertyChangeType::ROTATION_UPDATE_PROPERTY_ONLY_NOT_NOTIFY) {
412 return;
413 }
414 auto screenSession = GetScreenSession(screenId);
415 if (!screenSession) {
416 TLOGE(WmsLogTag::DMS, "screenSession is null");
417 return;
418 }
419 auto foldDisplayMode = screenSessionManager_->GetFoldDisplayMode();
420 screenSession->SetPhysicalRotation(directionInfo.phyRotation_);
421 screenSession->SetScreenComponentRotation(directionInfo.screenRotation_);
422 screenSession->UpdateToInputManager(bounds, directionInfo.notifyRotation_, directionInfo.rotation_,
423 foldDisplayMode);
424 screenSession->UpdateTouchBoundsAndOffset();
425 TLOGW(WmsLogTag::DMS, "superFoldStatus:%{public}d", currentstate_);
426 if (currentstate_ != SuperFoldStatus::KEYBOARD) {
427 screenSession->SetValidHeight(bounds.rect_.GetHeight());
428 screenSession->SetValidWidth(bounds.rect_.GetWidth());
429 }
430 }
431
SetDisplayNodeScreenId(ScreenId screenId,ScreenId displayNodeScreenId)432 void ScreenSessionManagerClient::SetDisplayNodeScreenId(ScreenId screenId, ScreenId displayNodeScreenId)
433 {
434 auto screenSession = GetScreenSession(screenId);
435 if (!screenSession) {
436 TLOGE(WmsLogTag::DMS, "screenSession is null");
437 return;
438 }
439 screenSession->SetDisplayNodeScreenId(displayNodeScreenId);
440 }
441
GetCurvedCompressionArea()442 uint32_t ScreenSessionManagerClient::GetCurvedCompressionArea()
443 {
444 if (!screenSessionManager_) {
445 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
446 return 0;
447 }
448 return screenSessionManager_->GetCurvedCompressionArea();
449 }
450
GetPhyScreenProperty(ScreenId screenId)451 ScreenProperty ScreenSessionManagerClient::GetPhyScreenProperty(ScreenId screenId)
452 {
453 if (!screenSessionManager_) {
454 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
455 return {};
456 }
457 return screenSessionManager_->GetPhyScreenProperty(screenId);
458 }
459
NotifyDisplayChangeInfoChanged(const sptr<DisplayChangeInfo> & info)460 __attribute__((no_sanitize("cfi"))) void ScreenSessionManagerClient::NotifyDisplayChangeInfoChanged(
461 const sptr<DisplayChangeInfo>& info)
462 {
463 if (!screenSessionManager_) {
464 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
465 return;
466 }
467 screenSessionManager_->NotifyDisplayChangeInfoChanged(info);
468 }
469
SetScreenPrivacyState(bool hasPrivate)470 void ScreenSessionManagerClient::SetScreenPrivacyState(bool hasPrivate)
471 {
472 if (!screenSessionManager_) {
473 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
474 return;
475 }
476 TLOGD(WmsLogTag::DMS, "Begin calling the SetScreenPrivacyState() of screenSessionManager_, hasPrivate: %{public}d",
477 hasPrivate);
478 screenSessionManager_->SetScreenPrivacyState(hasPrivate);
479 TLOGD(WmsLogTag::DMS, "End calling the SetScreenPrivacyState() of screenSessionManager_");
480 }
481
SetPrivacyStateByDisplayId(DisplayId id,bool hasPrivate)482 void ScreenSessionManagerClient::SetPrivacyStateByDisplayId(DisplayId id, bool hasPrivate)
483 {
484 if (!screenSessionManager_) {
485 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
486 return;
487 }
488 TLOGD(WmsLogTag::DMS, "Begin calling the SetPrivacyStateByDisplayId, hasPrivate: %{public}d", hasPrivate);
489 screenSessionManager_->SetPrivacyStateByDisplayId(id, hasPrivate);
490 TLOGD(WmsLogTag::DMS, "End calling the SetPrivacyStateByDisplayId");
491 }
492
SetScreenPrivacyWindowList(DisplayId id,std::vector<std::string> privacyWindowList)493 void ScreenSessionManagerClient::SetScreenPrivacyWindowList(DisplayId id, std::vector<std::string> privacyWindowList)
494 {
495 if (!screenSessionManager_) {
496 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
497 return;
498 }
499 TLOGD(WmsLogTag::DMS, "Begin calling the SetScreenPrivacyWindowList(), id: %{public}" PRIu64, id);
500 screenSessionManager_->SetScreenPrivacyWindowList(id, privacyWindowList);
501 TLOGD(WmsLogTag::DMS, "End calling the SetScreenPrivacyWindowList()");
502 }
503
UpdateAvailableArea(ScreenId screenId,DMRect area)504 void ScreenSessionManagerClient::UpdateAvailableArea(ScreenId screenId, DMRect area)
505 {
506 if (!screenSessionManager_) {
507 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
508 return;
509 }
510 screenSessionManager_->UpdateAvailableArea(screenId, area);
511 }
512
UpdateSuperFoldAvailableArea(ScreenId screenId,DMRect bArea,DMRect cArea)513 void ScreenSessionManagerClient::UpdateSuperFoldAvailableArea(ScreenId screenId, DMRect bArea, DMRect cArea)
514 {
515 if (!screenSessionManager_) {
516 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
517 return;
518 }
519 screenSessionManager_->UpdateSuperFoldAvailableArea(screenId, bArea, cArea);
520 }
521
UpdateSuperFoldExpandAvailableArea(ScreenId screenId,DMRect area)522 void ScreenSessionManagerClient::UpdateSuperFoldExpandAvailableArea(ScreenId screenId, DMRect area)
523 {
524 if (!screenSessionManager_) {
525 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
526 return;
527 }
528 screenSessionManager_->UpdateSuperFoldExpandAvailableArea(screenId, area);
529 }
530
SetScreenOffDelayTime(int32_t delay)531 int32_t ScreenSessionManagerClient::SetScreenOffDelayTime(int32_t delay)
532 {
533 if (!screenSessionManager_) {
534 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
535 return 0;
536 }
537 return screenSessionManager_->SetScreenOffDelayTime(delay);
538 }
539
SetScreenOnDelayTime(int32_t delay)540 int32_t ScreenSessionManagerClient::SetScreenOnDelayTime(int32_t delay)
541 {
542 if (!screenSessionManager_) {
543 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
544 return 0;
545 }
546 return screenSessionManager_->SetScreenOnDelayTime(delay);
547 }
548
SetCameraStatus(int32_t cameraStatus,int32_t cameraPosition)549 void ScreenSessionManagerClient::SetCameraStatus(int32_t cameraStatus, int32_t cameraPosition)
550 {
551 if (!screenSessionManager_) {
552 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
553 return;
554 }
555 return screenSessionManager_->SetCameraStatus(cameraStatus, cameraPosition);
556 }
557
NotifyFoldToExpandCompletion(bool foldToExpand)558 void ScreenSessionManagerClient::NotifyFoldToExpandCompletion(bool foldToExpand)
559 {
560 if (!screenSessionManager_) {
561 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
562 return;
563 }
564 screenSessionManager_->NotifyFoldToExpandCompletion(foldToExpand);
565 }
566
NotifyScreenConnectCompletion(ScreenId screenId)567 void ScreenSessionManagerClient::NotifyScreenConnectCompletion(ScreenId screenId)
568 {
569 if (!screenSessionManager_) {
570 TLOGE(WmsLogTag::DMS, "screenSessionManager is null");
571 return;
572 }
573 screenSessionManager_->NotifyScreenConnectCompletion(screenId);
574 }
575
RecordEventFromScb(std::string description,bool needRecordEvent)576 void ScreenSessionManagerClient::RecordEventFromScb(std::string description, bool needRecordEvent)
577 {
578 if (!screenSessionManager_) {
579 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
580 return;
581 }
582 screenSessionManager_->RecordEventFromScb(description, needRecordEvent);
583 }
584
SwitchUserCallback(std::vector<int32_t> oldScbPids,int32_t currentScbPid)585 void ScreenSessionManagerClient::SwitchUserCallback(std::vector<int32_t> oldScbPids, int32_t currentScbPid)
586 {
587 if (screenSessionManager_ == nullptr) {
588 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
589 return;
590 }
591 if (oldScbPids.size() == 0) {
592 TLOGE(WmsLogTag::DMS, "oldScbPids size 0");
593 return;
594 }
595 std::map<ScreenId, sptr<ScreenSession>> screenSessionMapCopy;
596 {
597 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
598 screenSessionMapCopy = screenSessionMap_;
599 }
600 for (const auto& iter : screenSessionMapCopy) {
601 ScreenId screenId = iter.first;
602 sptr<ScreenSession> screenSession = iter.second;
603 if (screenSession == nullptr) {
604 TLOGE(WmsLogTag::DMS, "screenSession is null");
605 continue;
606 }
607 {
608 auto displayNode = screenSessionManager_->GetDisplayNode(screenId);
609 if (displayNode == nullptr) {
610 TLOGE(WmsLogTag::DMS, "display node is null");
611 continue;
612 }
613 RSAdapterUtil::SetRSUIContext(displayNode, screenSession->GetRSUIContext(), true);
614 displayNode->SetScbNodePid(oldScbPids, currentScbPid);
615 RSTransactionAdapter::FlushImplicitTransaction(displayNode);
616 }
617 ScreenProperty screenProperty = screenSession->GetScreenProperty();
618 RRect bounds = screenProperty.GetBounds();
619 float rotation = screenSession->ConvertRotationToFloat(screenSession->GetRotation());
620 if (FoldScreenStateInternel::IsSuperFoldDisplayDevice()) {
621 UpdatePropertyWhenSwitchUser(screenSession, rotation, bounds, screenId);
622 } else {
623 screenSessionManager_->UpdateScreenRotationProperty(screenId, bounds, rotation,
624 ScreenPropertyChangeType::ROTATION_UPDATE_PROPERTY_ONLY, true);
625 }
626 }
627 TLOGI(WmsLogTag::DMS, "switch user callback end");
628 }
629
SwitchingCurrentUser()630 void ScreenSessionManagerClient::SwitchingCurrentUser()
631 {
632 if (screenSessionManager_ == nullptr) {
633 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
634 return;
635 }
636 screenSessionManager_->SwitchUser();
637 TLOGI(WmsLogTag::DMS, "switch to current user end");
638 }
639
DisconnectAllExternalScreen()640 void ScreenSessionManagerClient::DisconnectAllExternalScreen()
641 {
642 ScreenId setScreenId = SCREEN_ID_INVALID;
643 {
644 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
645 for (auto sessionIt = screenSessionMap_.rbegin(); sessionIt != screenSessionMap_.rend(); ++sessionIt) {
646 auto screenSession = sessionIt->second;
647 if (screenSession == nullptr) {
648 TLOGE(WmsLogTag::DMS, "screenSession is nullptr!");
649 continue;
650 }
651 if (screenSession->GetScreenProperty().GetScreenType() ==
652 ScreenType::REAL && screenSession->GetIsExtend()) {
653 TLOGI(WmsLogTag::DMS, "disconnect extend screen, screenId = %{public}" PRIu64, sessionIt->first);
654 screenSession->DestroyScreenScene();
655 NotifyScreenDisconnect(screenSession);
656 ScreenId screenId = sessionIt->first;
657 screenSessionMap_.erase(screenId);
658 setScreenId = screenId;
659 screenSession->Disconnect();
660 break;
661 }
662 }
663 }
664 if (setScreenId != SCREEN_ID_INVALID) {
665 std::unique_lock<std::mutex> lock(screenEventMutex_);
666 connectedScreenSet_.erase(setScreenId);
667 }
668 }
669
GetFoldStatus()670 FoldStatus ScreenSessionManagerClient::GetFoldStatus()
671 {
672 if (!screenSessionManager_) {
673 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
674 return FoldStatus::UNKNOWN;
675 }
676 return screenSessionManager_->GetFoldStatus();
677 }
678
GetSuperFoldStatus()679 SuperFoldStatus ScreenSessionManagerClient::GetSuperFoldStatus()
680 {
681 if (!screenSessionManager_) {
682 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
683 return SuperFoldStatus::UNKNOWN;
684 }
685 return screenSessionManager_->GetSuperFoldStatus();
686 }
687
GetSuperRotation()688 float ScreenSessionManagerClient::GetSuperRotation()
689 {
690 if (!screenSessionManager_) {
691 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
692 return -1.f;
693 }
694 return screenSessionManager_->GetSuperRotation();
695 }
696
SetLandscapeLockStatus(bool isLocked)697 void ScreenSessionManagerClient::SetLandscapeLockStatus(bool isLocked)
698 {
699 if (!screenSessionManager_) {
700 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
701 return;
702 }
703 return screenSessionManager_->SetLandscapeLockStatus(isLocked);
704 }
705
SetForceCloseHdr(ScreenId screenId,bool isForceCloseHdr)706 void ScreenSessionManagerClient::SetForceCloseHdr(ScreenId screenId, bool isForceCloseHdr)
707 {
708 if (!screenSessionManager_) {
709 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
710 return;
711 }
712 return screenSessionManager_->SetForceCloseHdr(screenId, isForceCloseHdr);
713 }
714
GetExtendScreenConnectStatus()715 ExtendScreenConnectStatus ScreenSessionManagerClient::GetExtendScreenConnectStatus()
716 {
717 if (!screenSessionManager_) {
718 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
719 return ExtendScreenConnectStatus::UNKNOWN;
720 }
721 return screenSessionManager_->GetExtendScreenConnectStatus();
722 }
723
GetScreenSnapshot(ScreenId screenId,float scaleX,float scaleY)724 std::shared_ptr<Media::PixelMap> ScreenSessionManagerClient::GetScreenSnapshot(ScreenId screenId,
725 float scaleX, float scaleY)
726 {
727 auto screenSession = GetScreenSession(screenId);
728 if (!screenSession) {
729 TLOGE(WmsLogTag::DMS, "get screen session is null");
730 return nullptr;
731 }
732 return screenSession->GetScreenSnapshot(scaleX, scaleY);
733 }
734
GetDeviceScreenConfig()735 DeviceScreenConfig ScreenSessionManagerClient::GetDeviceScreenConfig()
736 {
737 if (!screenSessionManager_) {
738 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
739 return {};
740 }
741 return screenSessionManager_->GetDeviceScreenConfig();
742 }
743
GetScreenSessionById(const ScreenId id)744 sptr<ScreenSession> ScreenSessionManagerClient::GetScreenSessionById(const ScreenId id)
745 {
746 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
747 auto iter = screenSessionMap_.find(id);
748 if (iter == screenSessionMap_.end()) {
749 return nullptr;
750 }
751 return iter->second;
752 }
753
GetRSUIDirector(ScreenId screenId)754 std::shared_ptr<RSUIDirector> ScreenSessionManagerClient::GetRSUIDirector(ScreenId screenId)
755 {
756 RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED(nullptr);
757 if (screenId == SCREEN_ID_INVALID) {
758 screenId = GetDefaultScreenId();
759 TLOGW(WmsLogTag::WMS_SCB,
760 "screenId is invalid, use default screenId: %{public}" PRIu64, screenId);
761 }
762 if (screenId == SCREEN_ID_INVALID) {
763 TLOGW(WmsLogTag::WMS_SCB, "Default screenId is also invalid");
764 return nullptr;
765 }
766 auto screenSession = GetScreenSession(screenId);
767 if (!screenSession) {
768 TLOGE(WmsLogTag::WMS_SCB,
769 "Screen session is null, screenId: %{public}" PRIu64, screenId);
770 return nullptr;
771 }
772 auto rsUIDirector = screenSession->GetRSUIDirector();
773 TLOGD(WmsLogTag::WMS_SCB,
774 "%{public}s, screenId: %{public}" PRIu64,
775 RSAdapterUtil::RSUIDirectorToStr(rsUIDirector).c_str(), screenId);
776 return rsUIDirector;
777 }
778
GetRSUIContext(ScreenId screenId)779 std::shared_ptr<RSUIContext> ScreenSessionManagerClient::GetRSUIContext(ScreenId screenId)
780 {
781 RETURN_IF_RS_CLIENT_MULTI_INSTANCE_DISABLED(nullptr);
782 auto rsUIDirector = GetRSUIDirector(screenId);
783 auto rsUIContext = rsUIDirector ? rsUIDirector->GetRSUIContext() : nullptr;
784 TLOGD(WmsLogTag::WMS_SCB, "%{public}s, screenId: %{public}" PRIu64,
785 RSAdapterUtil::RSUIContextToStr(rsUIContext).c_str(), screenId);
786 return rsUIContext;
787 }
788
GetDefaultScreenId()789 ScreenId ScreenSessionManagerClient::GetDefaultScreenId()
790 {
791 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
792 auto iter = screenSessionMap_.begin();
793 if (iter != screenSessionMap_.end()) {
794 return iter->first;
795 }
796 return SCREEN_ID_INVALID;
797 }
798
IsFoldable()799 bool ScreenSessionManagerClient::IsFoldable()
800 {
801 if (!screenSessionManager_) {
802 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
803 return false;
804 }
805 return screenSessionManager_->IsFoldable();
806 }
807
SetVirtualPixelRatioSystem(ScreenId screenId,float virtualPixelRatio)808 void ScreenSessionManagerClient::SetVirtualPixelRatioSystem(ScreenId screenId, float virtualPixelRatio)
809 {
810 sptr<ScreenSession> screenSession = GetScreenSession(screenId);
811 if (!screenSession) {
812 TLOGE(WmsLogTag::DMS, "screen session is null");
813 return;
814 }
815 if (screenSession->isScreenGroup_) {
816 TLOGE(WmsLogTag::DMS, "cannot set virtual pixel ratio to the combination. screen: %{public}" PRIu64, screenId);
817 return;
818 }
819 screenSession->SetScreenSceneDpi(virtualPixelRatio);
820 }
821
UpdateDisplayHookInfo(int32_t uid,bool enable,const DMHookInfo & hookInfo)822 void ScreenSessionManagerClient::UpdateDisplayHookInfo(int32_t uid, bool enable, const DMHookInfo& hookInfo)
823 {
824 if (!screenSessionManager_) {
825 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
826 return;
827 }
828 screenSessionManager_->UpdateDisplayHookInfo(uid, enable, hookInfo);
829 }
830
GetDisplayHookInfo(int32_t uid,DMHookInfo & hookInfo) const831 void ScreenSessionManagerClient::GetDisplayHookInfo(int32_t uid, DMHookInfo& hookInfo) const
832 {
833 if (!screenSessionManager_) {
834 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
835 return;
836 }
837 screenSessionManager_->GetDisplayHookInfo(uid, hookInfo);
838 }
839
OnFoldStatusChangedReportUE(const std::vector<std::string> & screenFoldInfo)840 void ScreenSessionManagerClient::OnFoldStatusChangedReportUE(const std::vector<std::string>& screenFoldInfo)
841 {
842 if (displayChangeListener_) {
843 displayChangeListener_->OnScreenFoldStatusChanged(screenFoldInfo);
844 }
845 }
846
UpdateDisplayScale(ScreenId id,float scaleX,float scaleY,float pivotX,float pivotY,float translateX,float translateY)847 void ScreenSessionManagerClient::UpdateDisplayScale(ScreenId id, float scaleX, float scaleY, float pivotX, float pivotY,
848 float translateX, float translateY)
849 {
850 auto session = GetScreenSession(id);
851 if (session == nullptr) {
852 TLOGE(WmsLogTag::DMS, "session is null");
853 return;
854 }
855 auto displayNode = session->GetDisplayNode();
856 if (displayNode == nullptr) {
857 TLOGE(WmsLogTag::DMS, "displayNode is null");
858 return;
859 }
860 TLOGW(WmsLogTag::DMS, "scale [%{public}f, %{public}f] translate [%{public}f, %{public}f]", scaleX, scaleY,
861 translateX, translateY);
862 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER,
863 "ssmc:UpdateDisplayScale(ScreenId = %" PRIu64
864 " scaleX=%f, scaleY=%f, pivotX=%f, pivotY=%f, translateX=%f, translateY=%f",
865 id, scaleX, scaleY, pivotX, pivotY, translateX, translateY);
866 session->SetScreenScale(scaleX, scaleY, pivotX, pivotY, translateX, translateY);
867 session->PropertyChange(session->GetScreenProperty(), ScreenPropertyChangeReason::ACCESS_INFO_CHANGE);
868 }
869
ScreenCaptureNotify(ScreenId mainScreenId,int32_t uid,const std::string & clientName)870 void ScreenSessionManagerClient::ScreenCaptureNotify(ScreenId mainScreenId, int32_t uid, const std::string& clientName)
871 {
872 sptr<ScreenSession> screenSession = GetScreenSession(mainScreenId);
873 if (!screenSession) {
874 TLOGE(WmsLogTag::DMS, "screen session is null");
875 return;
876 }
877 TLOGI(WmsLogTag::DMS, "capture screenId: %{public}" PRIu64", uid=%{public}d", mainScreenId, uid);
878 screenSession->ScreenCaptureNotify(mainScreenId, uid, clientName);
879 }
880
OnSuperFoldStatusChanged(ScreenId screenId,SuperFoldStatus superFoldStatus)881 void ScreenSessionManagerClient::OnSuperFoldStatusChanged(ScreenId screenId, SuperFoldStatus superFoldStatus)
882 {
883 currentstate_ = superFoldStatus;
884 auto screenSession = GetScreenSession(screenId);
885 if (!screenSession) {
886 TLOGE(WmsLogTag::DMS, "screenSession is null");
887 return;
888 }
889 TLOGI(WmsLogTag::DMS, "screenId=%{public}" PRIu64 " superFoldStatus=%{public}d", screenId,
890 static_cast<uint32_t>(superFoldStatus));
891 screenSession->SuperFoldStatusChange(screenId, superFoldStatus);
892 }
893
OnSecondaryReflexionChanged(ScreenId screenId,bool isSecondaryReflexion)894 void ScreenSessionManagerClient::OnSecondaryReflexionChanged(ScreenId screenId, bool isSecondaryReflexion)
895 {
896 auto screenSession = GetScreenSession(screenId);
897 if (!screenSession) {
898 TLOGE(WmsLogTag::DMS, "screenSession is null");
899 return;
900 }
901 TLOGI(WmsLogTag::DMS, "screenId=%{public}" PRIu64 " isSecondaryReflexion=%{public}d", screenId,
902 isSecondaryReflexion);
903 screenSession->SecondaryReflexionChange(screenId, isSecondaryReflexion);
904 }
905
OnExtendScreenConnectStatusChanged(ScreenId screenId,ExtendScreenConnectStatus extendScreenConnectStatus)906 void ScreenSessionManagerClient::OnExtendScreenConnectStatusChanged(ScreenId screenId,
907 ExtendScreenConnectStatus extendScreenConnectStatus)
908 {
909 auto screenSession = GetScreenSession(GetDefaultScreenId());
910 if (!screenSession) {
911 TLOGE(WmsLogTag::DMS, "screenSession is null");
912 return;
913 }
914 TLOGI(WmsLogTag::DMS, "screenId=%{public}" PRIu64 " extendScreenConnectStatus=%{public}d", screenId,
915 static_cast<uint32_t>(extendScreenConnectStatus));
916 screenSession->ExtendScreenConnectStatusChange(screenId, extendScreenConnectStatus);
917 }
918
UpdatePropertyWhenSwitchUser(const sptr<ScreenSession> & screenSession,float rotation,RRect bounds,ScreenId screenId)919 void ScreenSessionManagerClient::UpdatePropertyWhenSwitchUser(const sptr <ScreenSession>& screenSession,
920 float rotation, RRect bounds, ScreenId screenId)
921 {
922 currentstate_ = GetSuperFoldStatus();
923 screenSession->SetPointerActiveWidth(0);
924 screenSession->SetPointerActiveHeight(0);
925 screenSession->UpdateToInputManager(bounds, static_cast<int>(rotation), static_cast<int>(rotation),
926 FoldDisplayMode::UNKNOWN);
927 screenSession->SetPhysicalRotation(rotation);
928 screenSession->SetScreenComponentRotation(rotation);
929 ScreenDirectionInfo directionInfo;
930 directionInfo.screenRotation_ = static_cast<int32_t>(rotation);
931 directionInfo.rotation_ = static_cast<int32_t>(rotation);
932 directionInfo.phyRotation_ = static_cast<int32_t>(rotation);
933 screenSessionManager_->UpdateScreenDirectionInfo(screenId, directionInfo,
934 ScreenPropertyChangeType::UNSPECIFIED, bounds);
935 screenSessionManager_->UpdateScreenRotationProperty(screenId, bounds, rotation,
936 ScreenPropertyChangeType::UNSPECIFIED, true);
937 ScreenProperty property = screenSessionManager_->GetScreenProperty(screenId);
938 if (property.GetValidHeight() == INT32_MAX || property.GetValidHeight() == 0) {
939 TLOGW(WmsLogTag::DMS, "invalid property, validheight is bounds");
940 screenSession->SetValidHeight(bounds.rect_.GetHeight());
941 } else {
942 screenSession->SetValidHeight(property.GetValidHeight());
943 }
944 if (property.GetValidWidth() == INT32_MAX || property.GetValidWidth() == 0) {
945 TLOGW(WmsLogTag::DMS, "invalid property, validwidth is bounds");
946 screenSession->SetValidWidth(bounds.rect_.GetWidth());
947 } else {
948 screenSession->SetValidWidth(property.GetValidWidth());
949 }
950 TLOGI(WmsLogTag::DMS, "get property by screenId=%{public}" PRIu64 ", "
951 "bounds width=%{public}f, bounds height=%{public}f, "
952 "pointerActiveWidth=%{public}u, pointerActiveHeight=%{public}u, "
953 "validWidth=%{public}d, validHeight=%{public}d",
954 screenId, bounds.rect_.GetWidth(), bounds.rect_.GetHeight(),
955 screenSession->GetPointerActiveWidth(), screenSession->GetPointerActiveHeight(),
956 screenSession->GetValidWidth(), screenSession->GetValidHeight());
957 }
958
NotifyClientScreenConnect(sptr<ScreenSession> & screenSession)959 void ScreenSessionManagerClient::NotifyClientScreenConnect(sptr<ScreenSession>& screenSession)
960 {
961 NotifyScreenConnect(screenSession);
962 if (screenConnectionListener_) {
963 TLOGW(WmsLogTag::DMS, "screenId: %{public}" PRIu64 " density: %{public}f ",
964 screenSession->GetScreenId(), screenSession->GetScreenProperty().GetDensity());
965 screenSession->SetScreenSceneDpi(screenSession->GetScreenProperty().GetDensity());
966 }
967 screenSession->Connect();
968 }
969
HandleScreenConnection(SessionOption option)970 bool ScreenSessionManagerClient::HandleScreenConnection(SessionOption option)
971 {
972 if (!CheckIfNeedConnectScreen(option)) {
973 TLOGW(WmsLogTag::DMS, "no need to connect the screen");
974 return false;
975 }
976 sptr<ScreenSession> screenSession = nullptr;
977 auto iter = screenSessionMap_.find(option.screenId_);
978 if (iter != screenSessionMap_.end() && iter->second != nullptr) {
979 TLOGW(WmsLogTag::DMS, "screen session has exist.");
980 screenSession = iter->second;
981 }
982 ScreenSessionConfig config = {
983 .screenId = option.screenId_,
984 .rsId = option.rsId_,
985 .name = option.name_,
986 .innerName = option.innerName_,
987 };
988 config.property = screenSessionManager_->GetScreenProperty(option.screenId_);
989 TLOGW(WmsLogTag::DMS, "width:%{public}f, height=%{public}f",
990 config.property.GetBounds().rect_.GetWidth(), config.property.GetBounds().rect_.GetHeight());
991 config.displayNode = screenSessionManager_->GetDisplayNode(option.screenId_);
992 if (screenSession == nullptr) {
993 screenSession = new ScreenSession(config, ScreenSessionReason::CREATE_SESSION_FOR_CLIENT);
994 } else {
995 screenSession->SetScreenProperty(config.property);
996 screenSession->SetDisplayNode(config.displayNode);
997 }
998 screenSession->SetScreenCombination(screenSessionManager_->GetScreenCombination(option.screenId_));
999 screenSession->SetIsExtend(option.isExtend_);
1000 screenSession->SetIsRealScreen(screenSessionManager_->GetIsRealScreen(option.screenId_));
1001 {
1002 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
1003 screenSessionMap_[option.screenId_] = screenSession;
1004 extraScreenSessionMap_[option.screenId_] = screenSession;
1005 }
1006 NotifyClientScreenConnect(screenSession);
1007 return true;
1008 }
1009
HandleScreenDisconnection(SessionOption option)1010 bool ScreenSessionManagerClient::HandleScreenDisconnection(SessionOption option)
1011 {
1012 auto screenSession = GetScreenSession(option.screenId_);
1013 if (!screenSession) {
1014 TLOGE(WmsLogTag::DMS, "screenSession is null");
1015 return false;
1016 }
1017 screenSession->DestroyScreenScene();
1018 NotifyScreenDisconnect(screenSession);
1019 {
1020 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
1021 screenSessionMap_.erase(option.screenId_);
1022 }
1023 TLOGW(WmsLogTag::DMS, "disconnect screenId=%{public}" PRIu64, screenSession->GetScreenId());
1024 screenSession->Disconnect();
1025 return true;
1026 }
1027
OnCreateScreenSessionOnly(ScreenId screenId,ScreenId rsId,const std::string & name,bool isExtend)1028 bool ScreenSessionManagerClient::OnCreateScreenSessionOnly(ScreenId screenId, ScreenId rsId,
1029 const std::string& name, bool isExtend)
1030 {
1031 sptr<ScreenSession> screenSession = nullptr;
1032 ScreenSessionConfig config = {
1033 .screenId = screenId,
1034 .rsId = rsId,
1035 .name = name,
1036 };
1037 {
1038 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
1039 auto iter = screenSessionMap_.find(screenId);
1040 TLOGW(WmsLogTag::DMS, "screenId=%{public}" PRIu64" screen session size:%{public}d",
1041 screenId, static_cast<int>(screenSessionMap_.size()));
1042
1043 if (iter != screenSessionMap_.end() && iter->second != nullptr) {
1044 screenSession = iter->second;
1045 TLOGW(WmsLogTag::DMS, "screen session has exist.");
1046 }
1047 }
1048 if (screenSessionManager_ == nullptr) {
1049 TLOGE(WmsLogTag::DMS, "Failed to create screen session, screenSessionManager_ is null");
1050 return false;
1051 }
1052 config.displayNode = screenSessionManager_->GetDisplayNode(screenId);
1053 if (screenSession == nullptr) {
1054 config.property = screenSessionManager_->GetScreenProperty(screenId);
1055 screenSession = new ScreenSession(config, ScreenSessionReason::CREATE_SESSION_FOR_CLIENT);
1056 } else {
1057 screenSession->SetDisplayNode(config.displayNode);
1058 }
1059 screenSession->SetScreenCombination(screenSessionManager_->GetScreenCombination(screenId));
1060 screenSession->SetIsExtend(isExtend);
1061 screenSession->SetIsRealScreen(true);
1062 {
1063 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
1064 screenSessionMap_[screenId] = screenSession;
1065 extraScreenSessionMap_[screenId] = screenSession;
1066 }
1067 return true;
1068 }
1069
1070 /* main external */
OnExtendDisplayNodeChange(ScreenId mainScreenId,ScreenId extendScreenId)1071 bool ScreenSessionManagerClient::OnExtendDisplayNodeChange(ScreenId mainScreenId, ScreenId extendScreenId)
1072 {
1073 auto innerScreen = GetScreenSession(mainScreenId);
1074 auto externalScreen = GetScreenSession(extendScreenId);
1075 TLOGW(WmsLogTag::DMS, "mainScreenId=%{public}" PRIu64"; extendScreenId=%{public}" PRIu64,
1076 mainScreenId, extendScreenId);
1077 if (innerScreen == nullptr || externalScreen == nullptr) {
1078 TLOGE(WmsLogTag::DMS, "param is null");
1079 return false;
1080 }
1081 auto mainNode = innerScreen->GetDisplayNode();
1082 auto extendNode = externalScreen->GetDisplayNode();
1083 if (mainNode == nullptr || extendNode == nullptr) {
1084 TLOGE(WmsLogTag::DMS, "displayNode is null");
1085 return false;
1086 }
1087 ScreenId innerRSId = innerScreen->GetRSScreenId();
1088 ScreenId externalRSId = externalScreen->GetRSScreenId();
1089 std::ostringstream oss;
1090 oss << "innerScreen before screenId: " << mainScreenId
1091 << ", rsId: " << innerRSId
1092 << ", nodeId: " << mainNode->GetId()
1093 << ", externalScreen screenId: " << extendScreenId
1094 << ", rsId: " << externalRSId
1095 << ", nodeId: " << extendNode->GetId();
1096 oss << std::endl;
1097 TLOGW(WmsLogTag::DMS, "%{public}s", oss.str().c_str());
1098 /* change displayNode */
1099 mainNode->SetScreenId(externalRSId);
1100 extendNode->SetScreenId(innerRSId);
1101
1102 /* change rsId */
1103 innerScreen->SetRSScreenId(externalRSId);
1104 externalScreen->SetRSScreenId(innerRSId);
1105 RSTransactionAdapter::FlushImplicitTransaction(
1106 {innerScreen->GetRSUIContext(), externalScreen->GetRSUIContext()});
1107 oss.str("");
1108 oss << "innerScreen after screenId: " << mainScreenId
1109 << ", rsId: " << innerScreen->GetRSScreenId()
1110 << ", nodeId: " << mainNode->GetId()
1111 << ", externalScreen screenId: " << extendScreenId
1112 << ", rsId: " << externalScreen->GetRSScreenId()
1113 << ", nodeId: " << extendNode->GetId();
1114 oss << std::endl;
1115 TLOGW(WmsLogTag::DMS, "%{public}s", oss.str().c_str());
1116 return true;
1117 }
1118
CreateTempScreenSession(ScreenId screenId,ScreenId rsId,const std::shared_ptr<RSDisplayNode> & displayNode)1119 sptr<ScreenSession> ScreenSessionManagerClient::CreateTempScreenSession(
1120 ScreenId screenId, ScreenId rsId, const std::shared_ptr<RSDisplayNode>& displayNode)
1121 {
1122 if (screenSessionManager_ == nullptr) {
1123 TLOGE(WmsLogTag::DMS, "Failed to create temp screen session, screenSessionManager_ is null");
1124 return nullptr;
1125 }
1126 ScreenSessionConfig config = {
1127 .screenId = screenId,
1128 .rsId = rsId,
1129 .displayNode = displayNode,
1130 };
1131 config.property = screenSessionManager_->GetScreenProperty(screenId);
1132 TLOGW(WmsLogTag::DMS, "CreateTempScreenSession width:%{public}f, height=%{public}f",
1133 config.property.GetBounds().rect_.GetWidth(), config.property.GetBounds().rect_.GetHeight());
1134 return sptr<ScreenSession>::MakeSptr(config, ScreenSessionReason::CREATE_SESSION_FOR_CLIENT);
1135 }
1136
OnMainDisplayNodeChange(ScreenId mainScreenId,ScreenId extendScreenId,ScreenId extendRSId)1137 bool ScreenSessionManagerClient::OnMainDisplayNodeChange(ScreenId mainScreenId, ScreenId extendScreenId,
1138 ScreenId extendRSId)
1139 {
1140 auto mainScreen = GetScreenSession(mainScreenId);
1141 if (mainScreen == nullptr) {
1142 TLOGE(WmsLogTag::DMS, "mainScreen is null");
1143 return false;
1144 }
1145 if (screenSessionManager_ == nullptr) {
1146 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
1147 return false;
1148 }
1149 std::ostringstream oss;
1150 oss << "mainScreen mainScreenId: " << mainScreenId
1151 << ", extendScreenId: " << extendScreenId
1152 << ", extendRSId: " << extendRSId;
1153 oss << std::endl;
1154 TLOGW(WmsLogTag::DMS, "%{public}s", oss.str().c_str());
1155 auto mainNode = mainScreen->GetDisplayNode();
1156 auto extendNode = screenSessionManager_->GetDisplayNode(extendScreenId);
1157 if (mainNode == nullptr || extendNode == nullptr) {
1158 TLOGE(WmsLogTag::DMS, "displayNode is null");
1159 return false;
1160 }
1161 /* change displayNode */
1162 ScreenId innerRSId = mainScreen->GetRSScreenId();
1163 oss.str("");
1164 oss << "mainScreen mainScreenId: " << mainScreen->GetScreenId()
1165 << ", mainRSId: " << innerRSId
1166 << ", mainNodeId: " << mainNode->GetId()
1167 << ", extendNodeId: " << extendNode->GetId();
1168 oss << std::endl;
1169 TLOGW(WmsLogTag::DMS, "%{public}s", oss.str().c_str());
1170 auto tempScreenSession = CreateTempScreenSession(extendScreenId, innerRSId, extendNode);
1171 mainNode->SetScreenId(extendRSId);
1172 extendNode->SetScreenId(innerRSId);
1173
1174 /* change rsId */
1175 mainScreen->SetRSScreenId(extendRSId);
1176 RSTransactionAdapter::FlushImplicitTransaction({mainNode, extendNode});
1177 return true;
1178 }
1179
SetScreenCombination(ScreenId mainScreenId,ScreenId extendScreenId,ScreenCombination extendCombination)1180 void ScreenSessionManagerClient::SetScreenCombination(ScreenId mainScreenId, ScreenId extendScreenId,
1181 ScreenCombination extendCombination)
1182 {
1183 sptr<ScreenSession> screenSession = GetScreenSession(mainScreenId);
1184 if (!screenSession) {
1185 TLOGE(WmsLogTag::DMS, "screen session is null");
1186 return;
1187 }
1188 screenSession->SetScreenCombination(ScreenCombination::SCREEN_MAIN);
1189 screenSession = GetScreenSession(extendScreenId);
1190 if (!screenSession) {
1191 TLOGE(WmsLogTag::DMS, "screen session is null");
1192 return;
1193 }
1194 screenSession->SetScreenCombination(extendCombination);
1195 }
1196
OnDumperClientScreenSessions()1197 std::string ScreenSessionManagerClient::OnDumperClientScreenSessions()
1198 {
1199 std::ostringstream oss;
1200 oss << "-------------- Client Screen Infos --------------" << std::endl;
1201 {
1202 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
1203 for (const auto& iter : screenSessionMap_) {
1204 if (iter.second == nullptr) {
1205 oss << std::left << std::setw(LINE_WIDTH) << "session: " << "nullptr" << std::endl;
1206 continue;
1207 }
1208 ScreenProperty screenProperty = iter.second->GetScreenProperty();
1209 oss << std::left << std::setw(LINE_WIDTH) << "Name: " << iter.second->GetName() << std::endl;
1210 oss << std::left << std::setw(LINE_WIDTH) << "ScreenId: " << iter.second->GetScreenId() << std::endl;
1211 oss << std::left << std::setw(LINE_WIDTH) << "RSScreenId: " << iter.second->GetRSScreenId() << std::endl;
1212 if (iter.second->GetDisplayNode() != nullptr) {
1213 oss << std::left << std::setw(LINE_WIDTH) << "DisplayNode: "
1214 << iter.second->GetDisplayNode()->GetId() << std::endl;
1215 } else {
1216 oss << std::left << std::setw(LINE_WIDTH) << "DisplayNode: " << "nullptr" << std::endl;
1217 }
1218 oss << std::left << std::setw(LINE_WIDTH) << "ScreenCombination: "
1219 << static_cast<int32_t>(iter.second->GetScreenCombination()) << std::endl;
1220 oss << std::left << std::setw(LINE_WIDTH) << "isExtend: "
1221 << (iter.second->GetIsExtend() ? "true" : "false") << std::endl;
1222 oss << std::left << std::setw(LINE_WIDTH) << "Orientation: "
1223 << static_cast<int32_t>(screenProperty.GetOrientation()) << std::endl;
1224 oss << std::left << std::setw(LINE_WIDTH) << "Rotation: "
1225 << static_cast<int32_t>(screenProperty.GetRotation()) << std::endl;
1226 oss << std::left << std::setw(LINE_WIDTH) << "Bounds<L,T,W,H>: "
1227 << screenProperty.GetBounds().rect_.GetLeft() << ", "
1228 << screenProperty.GetBounds().rect_.GetTop() << ", "
1229 << screenProperty.GetBounds().rect_.GetWidth() << ", "
1230 << screenProperty.GetBounds().rect_.GetHeight() << ", " << std::endl;
1231 oss << std::left << std::setw(LINE_WIDTH) << "PhyBounds<L,T,W,H>: "
1232 << screenProperty.GetPhyBounds().rect_.GetLeft() << ", "
1233 << screenProperty.GetPhyBounds().rect_.GetTop() << ", "
1234 << screenProperty.GetPhyBounds().rect_.GetWidth() << ", "
1235 << screenProperty.GetPhyBounds().rect_.GetHeight() << ", " << std::endl;
1236 oss << std::left << std::setw(LINE_WIDTH) << "AvailableArea<X,Y,W,H> "
1237 << screenProperty.GetAvailableArea().posX_ << ", "
1238 << screenProperty.GetAvailableArea().posY_ << ", "
1239 << screenProperty.GetAvailableArea().width_ << ", "
1240 << screenProperty.GetAvailableArea().height_ << ", " << std::endl;
1241 oss << "------------------------------------------------" << std::endl;
1242 }
1243 }
1244 auto screenInfos = oss.str();
1245 TLOGW(WmsLogTag::DMS, "%{public}s", screenInfos.c_str());
1246 return screenInfos;
1247 }
1248
SetDefaultMultiScreenModeWhenSwitchUser()1249 void ScreenSessionManagerClient::SetDefaultMultiScreenModeWhenSwitchUser()
1250 {
1251 if (!screenSessionManager_) {
1252 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
1253 return;
1254 }
1255 screenSessionManager_->SetDefaultMultiScreenModeWhenSwitchUser();
1256 return;
1257 }
1258
NotifyExtendScreenCreateFinish()1259 void ScreenSessionManagerClient::NotifyExtendScreenCreateFinish()
1260 {
1261 if (!screenSessionManager_) {
1262 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
1263 return;
1264 }
1265 return screenSessionManager_->NotifyExtendScreenCreateFinish();
1266 }
1267
NotifyExtendScreenDestroyFinish()1268 void ScreenSessionManagerClient::NotifyExtendScreenDestroyFinish()
1269 {
1270 if (!screenSessionManager_) {
1271 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
1272 return;
1273 }
1274 return screenSessionManager_->NotifyExtendScreenDestroyFinish();
1275 }
1276
OnBeforeScreenPropertyChanged(FoldStatus foldStatus)1277 void ScreenSessionManagerClient::OnBeforeScreenPropertyChanged(FoldStatus foldStatus)
1278 {
1279 sptr<ScreenSession> screenSession = nullptr;
1280 {
1281 std::lock_guard<std::mutex> lock(screenSessionMapMutex_);
1282 if (screenSessionMap_.empty()) {
1283 TLOGE(WmsLogTag::DMS, "screenSessionMap_ is nullptr");
1284 return;
1285 }
1286 screenSession = screenSessionMap_.begin()->second;
1287 }
1288 if (!screenSession) {
1289 TLOGE(WmsLogTag::DMS, "screenSession is null");
1290 return;
1291 }
1292 TLOGI(WmsLogTag::DMS, "fold status %{public}d", foldStatus);
1293 screenSession->BeforeScreenPropertyChange(foldStatus);
1294 }
1295
OnScreenModeChanged(ScreenModeChangeEvent screenModeChangeEvent)1296 void ScreenSessionManagerClient::OnScreenModeChanged(ScreenModeChangeEvent screenModeChangeEvent)
1297 {
1298 auto screenSession = GetScreenSession(GetDefaultScreenId());
1299 if (!screenSession) {
1300 TLOGE(WmsLogTag::DMS, "screenSession is null");
1301 return;
1302 }
1303 TLOGI(WmsLogTag::DMS, "screenModeChangeEvent=%{public}d", static_cast<uint32_t>(screenModeChangeEvent));
1304 screenSession->ScreenModeChange(screenModeChangeEvent);
1305 }
1306
NotifyScreenMaskAppear()1307 void ScreenSessionManagerClient::NotifyScreenMaskAppear()
1308 {
1309 if (!screenSessionManager_) {
1310 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
1311 return;
1312 }
1313 return screenSessionManager_->NotifyScreenMaskAppear();
1314 }
1315
SetPrimaryDisplaySystemDpi(float dpi)1316 DMError ScreenSessionManagerClient::SetPrimaryDisplaySystemDpi(float dpi)
1317 {
1318 if (!screenSessionManager_) {
1319 TLOGE(WmsLogTag::DMS, "screenSessionManager_ is null");
1320 return DMError::DM_ERROR_NULLPTR;
1321 }
1322 return screenSessionManager_->SetPrimaryDisplaySystemDpi(dpi);
1323 }
1324
FreezeScreen(ScreenId screenId,bool isFreeze)1325 void ScreenSessionManagerClient::FreezeScreen(ScreenId screenId, bool isFreeze)
1326 {
1327 auto screenSession = GetScreenSession(screenId);
1328 if (!screenSession) {
1329 TLOGE(WmsLogTag::DMS, "get screen session is null, screenId is %{public}" PRIu64, screenId);
1330 return;
1331 }
1332 screenSession->FreezeScreen(isFreeze);
1333 }
1334
GetScreenSnapshotWithAllWindows(ScreenId screenId,float scaleX,float scaleY,bool isNeedCheckDrmAndSurfaceLock)1335 std::shared_ptr<Media::PixelMap> ScreenSessionManagerClient::GetScreenSnapshotWithAllWindows(ScreenId screenId,
1336 float scaleX, float scaleY, bool isNeedCheckDrmAndSurfaceLock)
1337 {
1338 auto screenSession = GetScreenSession(screenId);
1339 if (!screenSession) {
1340 TLOGE(WmsLogTag::DMS, "get screen session is null, screenId is %{public}" PRIu64, screenId);
1341 return nullptr;
1342 }
1343 return screenSession->GetScreenSnapshotWithAllWindows(scaleX, scaleY, isNeedCheckDrmAndSurfaceLock);
1344 }
1345 } // namespace OHOS::Rosen