• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "screen_manager.h"
16 
17 #include <map>
18 #include <vector>
19 
20 #include <transaction/rs_interfaces.h>
21 
22 #include "display_manager_adapter.h"
23 #include "display_manager_agent_default.h"
24 #include "permission.h"
25 #include "singleton_delegator.h"
26 #include "window_manager_hilog.h"
27 
28 
29 namespace OHOS::Rosen {
30 namespace {
31     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "ScreenManager"};
32     const static uint32_t MAX_SCREEN_SIZE = 32;
33 }
34 class ScreenManager::Impl : public RefBase {
35 public:
36     Impl() = default;
37     ~Impl();
38     static inline SingletonDelegator<ScreenManager> delegator;
39     ScreenId CreateVirtualScreen(VirtualScreenOption option);
40     sptr<Screen> GetScreen(ScreenId screenId);
41     sptr<ScreenGroup> GetScreenGroup(ScreenId screenId);
42     DMError GetAllScreens(std::vector<sptr<Screen>>& screens);
43     DMError RegisterScreenListener(sptr<IScreenListener> listener);
44     DMError UnregisterScreenListener(sptr<IScreenListener> listener);
45     DMError RegisterScreenGroupListener(sptr<IScreenGroupListener> listener);
46     DMError UnregisterScreenGroupListener(sptr<IScreenGroupListener> listener);
47     DMError RegisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener);
48     DMError UnregisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener);
49     DMError RegisterDisplayManagerAgent();
50     DMError UnregisterDisplayManagerAgent();
51     void OnRemoteDied();
52 
53 private:
54     void NotifyScreenConnect(sptr<ScreenInfo> info);
55     void NotifyScreenDisconnect(ScreenId);
56     void NotifyScreenChange(const sptr<ScreenInfo>& screenInfo);
57     void NotifyScreenChange(const std::vector<sptr<ScreenInfo>>& screenInfos);
58     bool UpdateScreenInfoLocked(sptr<ScreenInfo>);
59 
60     bool isAllListenersRemoved() const;
61 
62     class ScreenManagerListener;
63     sptr<ScreenManagerListener> screenManagerListener_;
64     std::map<ScreenId, sptr<Screen>> screenMap_;
65     std::map<ScreenId, sptr<ScreenGroup>> screenGroupMap_;
66     std::recursive_mutex mutex_;
67     std::set<sptr<IScreenListener>> screenListeners_;
68     std::set<sptr<IScreenGroupListener>> screenGroupListeners_;
69     std::set<sptr<IVirtualScreenGroupListener>> virtualScreenGroupListeners_;
70     sptr<IDisplayManagerAgent> virtualScreenAgent_ = nullptr;
71 };
72 
73 class ScreenManager::Impl::ScreenManagerListener : public DisplayManagerAgentDefault {
74 public:
ScreenManagerListener(sptr<Impl> impl)75     explicit ScreenManagerListener(sptr<Impl> impl) : pImpl_(impl)
76     {
77     }
78 
OnScreenConnect(sptr<ScreenInfo> screenInfo)79     void OnScreenConnect(sptr<ScreenInfo> screenInfo)
80     {
81         if (screenInfo == nullptr || screenInfo->GetScreenId() == SCREEN_ID_INVALID) {
82             WLOGFE("OnScreenConnect, screenInfo is invalid.");
83             return;
84         }
85         if (pImpl_ == nullptr) {
86             WLOGFE("OnScreenConnect, impl is nullptr.");
87             return;
88         }
89         pImpl_->NotifyScreenConnect(screenInfo);
90         std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
91         for (auto listener : pImpl_->screenListeners_) {
92             listener->OnConnect(screenInfo->GetScreenId());
93         }
94     };
95 
OnScreenDisconnect(ScreenId screenId)96     void OnScreenDisconnect(ScreenId screenId)
97     {
98         if (screenId == SCREEN_ID_INVALID) {
99             WLOGFE("OnScreenDisconnect, screenId is invalid.");
100             return;
101         }
102         if (pImpl_ == nullptr) {
103             WLOGFE("OnScreenDisconnect, impl is nullptr.");
104             return;
105         }
106         pImpl_->NotifyScreenDisconnect(screenId);
107         std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
108         for (auto listener : pImpl_->screenListeners_) {
109             listener->OnDisconnect(screenId);
110         }
111     };
112 
OnScreenChange(const sptr<ScreenInfo> & screenInfo,ScreenChangeEvent event)113     void OnScreenChange(const sptr<ScreenInfo>& screenInfo, ScreenChangeEvent event)
114     {
115         if (screenInfo == nullptr) {
116             WLOGFE("OnScreenChange, screenInfo is null.");
117             return;
118         }
119         if (pImpl_ == nullptr) {
120             WLOGFE("OnScreenChange, impl is nullptr.");
121             return;
122         }
123         WLOGFD("OnScreenChange. event %{public}u", event);
124         pImpl_->NotifyScreenChange(screenInfo);
125         std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
126         for (auto listener: pImpl_->screenListeners_) {
127             listener->OnChange(screenInfo->GetScreenId());
128         }
129     };
130 
OnScreenGroupChange(const std::string & trigger,const std::vector<sptr<ScreenInfo>> & screenInfos,ScreenGroupChangeEvent groupEvent)131     void OnScreenGroupChange(const std::string& trigger, const std::vector<sptr<ScreenInfo>>& screenInfos,
132         ScreenGroupChangeEvent groupEvent)
133     {
134         if (screenInfos.empty()) {
135             WLOGFE("screenInfos is empty.");
136             return;
137         }
138         if (pImpl_ == nullptr) {
139             WLOGFE("impl is nullptr.");
140             return;
141         }
142         WLOGFD("trigger %{public}s, event %{public}u", trigger.c_str(), groupEvent);
143         pImpl_->NotifyScreenChange(screenInfos);
144         std::vector<ScreenId> screenIds;
145         for (auto screenInfo : screenInfos) {
146             if (screenInfo->GetScreenId() != SCREEN_ID_INVALID) {
147                 screenIds.push_back(screenInfo->GetScreenId());
148             }
149         }
150         std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
151         for (auto listener: pImpl_->screenGroupListeners_) {
152             listener->OnChange(screenIds, groupEvent);
153         }
154         NotifyVirtualScreenGroupChanged(screenInfos[0], trigger, screenIds, groupEvent);
155     };
156 private:
NotifyVirtualScreenGroupChanged(sptr<ScreenInfo> screenInfo,const std::string trigger,std::vector<ScreenId> & ids,ScreenGroupChangeEvent groupEvent)157     void NotifyVirtualScreenGroupChanged(sptr<ScreenInfo> screenInfo,
158         const std::string trigger, std::vector<ScreenId>& ids, ScreenGroupChangeEvent groupEvent)
159     {
160         // check for invalid scene
161         if (pImpl_->virtualScreenGroupListeners_.size() <= 0) {
162             WLOGFW("no virtual screnn group listeners");
163             return;
164         }
165         if (screenInfo->GetType() != ScreenType::VIRTUAL) {
166             WLOGFW("not virtual screen type: %{public}u", screenInfo->GetType());
167             return;
168         }
169 
170         // get the parent of screen
171         ScreenId parent = groupEvent == ScreenGroupChangeEvent::ADD_TO_GROUP ?
172             screenInfo->GetParentId() : screenInfo->GetLastParentId();
173         WLOGFD("parentId=[%{public}llu], lastParentId=[%{public}llu]", (unsigned long long)screenInfo->GetParentId(),
174             (unsigned long long)screenInfo->GetLastParentId());
175         if (parent == INVALID_SCREEN_ID) {
176             WLOGFE("parentId is invalid");
177             return;
178         }
179         auto screenGroup = pImpl_->GetScreenGroup(parent);
180         if (screenGroup == nullptr) {
181             WLOGFE("screenGroup is null");
182             return;
183         }
184 
185         // notify mirror
186         ScreenCombination comb = screenGroup->GetCombination();
187         WLOGFD("comb %{public}u", comb);
188         IVirtualScreenGroupListener::ChangeInfo changeInfo = {groupEvent, trigger, ids};
189         for (auto listener: pImpl_->virtualScreenGroupListeners_) {
190             if (comb == ScreenCombination::SCREEN_MIRROR) {
191                 listener->OnMirrorChange(changeInfo);
192             }
193         }
194     }
195     sptr<Impl> pImpl_;
196 };
197 
WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManager)198 WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManager)
199 
200 ScreenManager::ScreenManager()
201 {
202     pImpl_ = new Impl();
203 }
204 
~ScreenManager()205 ScreenManager::~ScreenManager()
206 {
207 }
208 
~Impl()209 ScreenManager::Impl::~Impl()
210 {
211     std::lock_guard<std::recursive_mutex> lock(mutex_);
212     UnregisterDisplayManagerAgent();
213 }
214 
GetScreen(ScreenId screenId)215 sptr<Screen> ScreenManager::Impl::GetScreen(ScreenId screenId)
216 {
217     auto screenInfo = SingletonContainer::Get<ScreenManagerAdapter>().GetScreenInfo(screenId);
218     std::lock_guard<std::recursive_mutex> lock(mutex_);
219     if (!UpdateScreenInfoLocked(screenInfo)) {
220         screenMap_.erase(screenId);
221         return nullptr;
222     }
223     return screenMap_[screenId];
224 }
225 
GetScreenById(ScreenId screenId)226 sptr<Screen> ScreenManager::GetScreenById(ScreenId screenId)
227 {
228     return pImpl_->GetScreen(screenId);
229 }
230 
GetScreenGroup(ScreenId screenId)231 sptr<ScreenGroup> ScreenManager::Impl::GetScreenGroup(ScreenId screenId)
232 {
233     auto screenGroupInfo = SingletonContainer::Get<ScreenManagerAdapter>().GetScreenGroupInfoById(screenId);
234     std::lock_guard<std::recursive_mutex> lock(mutex_);
235     if (screenGroupInfo == nullptr) {
236         WLOGFE("screenGroupInfo is null");
237         screenGroupMap_.erase(screenId);
238         return nullptr;
239     }
240     auto iter = screenGroupMap_.find(screenId);
241     if (iter != screenGroupMap_.end() && iter->second != nullptr) {
242         iter->second->UpdateScreenGroupInfo(screenGroupInfo);
243         return iter->second;
244     }
245     sptr<ScreenGroup> screenGroup = new ScreenGroup(screenGroupInfo);
246     screenGroupMap_[screenId] = screenGroup;
247     return screenGroup;
248 }
249 
GetScreenGroup(ScreenId screenId)250 sptr<ScreenGroup> ScreenManager::GetScreenGroup(ScreenId screenId)
251 {
252     return pImpl_->GetScreenGroup(screenId);
253 }
254 
GetAllScreens(std::vector<sptr<Screen>> & screens)255 DMError ScreenManager::Impl::GetAllScreens(std::vector<sptr<Screen>>& screens)
256 {
257     std::vector<sptr<ScreenInfo>> screenInfos;
258     DMError ret  = SingletonContainer::Get<ScreenManagerAdapter>().GetAllScreenInfos(screenInfos);
259     std::lock_guard<std::recursive_mutex> lock(mutex_);
260     for (auto info: screenInfos) {
261         if (UpdateScreenInfoLocked(info)) {
262             screens.emplace_back(screenMap_[info->GetScreenId()]);
263         }
264     }
265     screenMap_.clear();
266     for (auto screen: screens) {
267         screenMap_.insert(std::make_pair(screen->GetId(), screen));
268     }
269     return ret;
270 }
271 
GetAllScreens(std::vector<sptr<Screen>> & screens)272 DMError ScreenManager::GetAllScreens(std::vector<sptr<Screen>>& screens)
273 {
274     return pImpl_->GetAllScreens(screens);
275 }
276 
RegisterScreenListener(sptr<IScreenListener> listener)277 DMError ScreenManager::Impl::RegisterScreenListener(sptr<IScreenListener> listener)
278 {
279     std::lock_guard<std::recursive_mutex> lock(mutex_);
280     DMError regSucc = RegisterDisplayManagerAgent();
281     if (regSucc == DMError::DM_OK) {
282         screenListeners_.insert(listener);
283     }
284     return regSucc;
285 }
286 
RegisterScreenListener(sptr<IScreenListener> listener)287 DMError ScreenManager::RegisterScreenListener(sptr<IScreenListener> listener)
288 {
289     if (listener == nullptr) {
290         WLOGFE("RegisterScreenListener listener is nullptr.");
291         return DMError::DM_ERROR_NULLPTR;
292     }
293     return pImpl_->RegisterScreenListener(listener);
294 }
295 
UnregisterScreenListener(sptr<IScreenListener> listener)296 DMError ScreenManager::Impl::UnregisterScreenListener(sptr<IScreenListener> listener)
297 {
298     std::lock_guard<std::recursive_mutex> lock(mutex_);
299     if (!Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
300         WLOGFE("unregister display manager agent permission denied!");
301         return DMError::DM_ERROR_NOT_SYSTEM_APP;
302     }
303     auto iter = std::find(screenListeners_.begin(), screenListeners_.end(), listener);
304     if (iter == screenListeners_.end()) {
305         WLOGFE("could not find this listener");
306         return DMError::DM_ERROR_NULLPTR;
307     }
308     screenListeners_.erase(iter);
309     return isAllListenersRemoved() ? UnregisterDisplayManagerAgent() : DMError::DM_OK;
310 }
311 
UnregisterScreenListener(sptr<IScreenListener> listener)312 DMError ScreenManager::UnregisterScreenListener(sptr<IScreenListener> listener)
313 {
314     if (listener == nullptr) {
315         WLOGFE("UnregisterScreenListener listener is nullptr.");
316         return DMError::DM_ERROR_NULLPTR;
317     }
318     return pImpl_->UnregisterScreenListener(listener);
319 }
320 
RegisterScreenGroupListener(sptr<IScreenGroupListener> listener)321 DMError ScreenManager::Impl::RegisterScreenGroupListener(sptr<IScreenGroupListener> listener)
322 {
323     std::lock_guard<std::recursive_mutex> lock(mutex_);
324     DMError regSucc = RegisterDisplayManagerAgent();
325     if (regSucc == DMError::DM_OK) {
326         screenGroupListeners_.insert(listener);
327     }
328     return regSucc;
329 }
330 
RegisterScreenGroupListener(sptr<IScreenGroupListener> listener)331 DMError ScreenManager::RegisterScreenGroupListener(sptr<IScreenGroupListener> listener)
332 {
333     if (listener == nullptr) {
334         WLOGFE("RegisterScreenGroupListener listener is nullptr.");
335         return DMError::DM_ERROR_NULLPTR;
336     }
337     return pImpl_->RegisterScreenGroupListener(listener);
338 }
339 
UnregisterScreenGroupListener(sptr<IScreenGroupListener> listener)340 DMError ScreenManager::Impl::UnregisterScreenGroupListener(sptr<IScreenGroupListener> listener)
341 {
342     std::lock_guard<std::recursive_mutex> lock(mutex_);
343     auto iter = std::find(screenGroupListeners_.begin(), screenGroupListeners_.end(), listener);
344     if (iter == screenGroupListeners_.end()) {
345         WLOGFE("could not find this listener");
346         return DMError::DM_ERROR_NULLPTR;
347     }
348     screenGroupListeners_.erase(iter);
349     return isAllListenersRemoved() ? UnregisterDisplayManagerAgent() : DMError::DM_OK;
350 }
351 
UnregisterScreenGroupListener(sptr<IScreenGroupListener> listener)352 DMError ScreenManager::UnregisterScreenGroupListener(sptr<IScreenGroupListener> listener)
353 {
354     if (listener == nullptr) {
355         WLOGFE("UnregisterScreenGroupListener listener is nullptr.");
356         return DMError::DM_ERROR_NULLPTR;
357     }
358     return pImpl_->UnregisterScreenGroupListener(listener);
359 }
360 
RegisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener)361 DMError ScreenManager::Impl::RegisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener)
362 {
363     std::lock_guard<std::recursive_mutex> lock(mutex_);
364     DMError regSucc = RegisterDisplayManagerAgent();
365     if (regSucc == DMError::DM_OK) {
366         virtualScreenGroupListeners_.insert(listener);
367     }
368     return regSucc;
369 }
370 
RegisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener)371 DMError ScreenManager::RegisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener)
372 {
373     if (listener == nullptr) {
374         WLOGFE("RegisterVirtualScreenGroupListener listener is nullptr.");
375         return DMError::DM_ERROR_NULLPTR;
376     }
377     return pImpl_->RegisterVirtualScreenGroupListener(listener);
378 }
379 
UnregisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener)380 DMError ScreenManager::Impl::UnregisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener)
381 {
382     std::lock_guard<std::recursive_mutex> lock(mutex_);
383     auto iter = std::find(virtualScreenGroupListeners_.begin(), virtualScreenGroupListeners_.end(), listener);
384     if (iter == virtualScreenGroupListeners_.end()) {
385         WLOGFE("could not find this listener");
386         return DMError::DM_ERROR_NULLPTR;
387     }
388     virtualScreenGroupListeners_.erase(iter);
389     return isAllListenersRemoved() ? UnregisterDisplayManagerAgent() : DMError::DM_OK;
390 }
391 
UnregisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener)392 DMError ScreenManager::UnregisterVirtualScreenGroupListener(sptr<IVirtualScreenGroupListener> listener)
393 {
394     if (listener == nullptr) {
395         WLOGFE("UnregisterVirtualScreenGroupListener listener is nullptr.");
396         return DMError::DM_ERROR_NULLPTR;
397     }
398     return pImpl_->UnregisterVirtualScreenGroupListener(listener);
399 }
400 
RegisterDisplayManagerAgent()401 DMError ScreenManager::Impl::RegisterDisplayManagerAgent()
402 {
403     DMError regSucc = DMError::DM_OK;
404     if (screenManagerListener_ == nullptr) {
405         screenManagerListener_ = new ScreenManagerListener(this);
406         regSucc = SingletonContainer::Get<ScreenManagerAdapter>().RegisterDisplayManagerAgent(
407             screenManagerListener_, DisplayManagerAgentType::SCREEN_EVENT_LISTENER);
408         if (regSucc != DMError::DM_OK) {
409             screenManagerListener_ = nullptr;
410             WLOGFW("RegisterDisplayManagerAgent failed !");
411         }
412     }
413     return regSucc;
414 }
415 
UnregisterDisplayManagerAgent()416 DMError ScreenManager::Impl::UnregisterDisplayManagerAgent()
417 {
418     DMError unRegSucc = DMError::DM_OK;
419     if (screenManagerListener_ != nullptr) {
420         unRegSucc = SingletonContainer::Get<ScreenManagerAdapter>().UnregisterDisplayManagerAgent(
421             screenManagerListener_, DisplayManagerAgentType::SCREEN_EVENT_LISTENER);
422         screenManagerListener_ = nullptr;
423         if (unRegSucc != DMError::DM_OK) {
424             WLOGFW("UnregisterDisplayManagerAgent failed!");
425         }
426     }
427     return unRegSucc;
428 }
429 
MakeExpand(const std::vector<ExpandOption> & options,ScreenId & screenGroupId)430 DMError ScreenManager::MakeExpand(const std::vector<ExpandOption>& options, ScreenId& screenGroupId)
431 {
432     WLOGFI("Make expand");
433     if (options.empty()) {
434         return DMError::DM_ERROR_INVALID_PARAM;
435     }
436     if (options.size() > MAX_SCREEN_SIZE) {
437         WLOGFW("Make expand failed. The options size is bigger than %{public}u.", MAX_SCREEN_SIZE);
438         return DMError::DM_ERROR_INVALID_PARAM;
439     }
440     std::vector<ScreenId> screenIds;
441     std::vector<Point> startPoints;
442     for (auto& option: options) {
443         if (std::find(screenIds.begin(), screenIds.end(), option.screenId_) != screenIds.end()) {
444             continue;
445         }
446         screenIds.emplace_back(option.screenId_);
447         startPoints.emplace_back(Point(option.startX_, option.startY_));
448     }
449     DMError ret = SingletonContainer::Get<ScreenManagerAdapter>().MakeExpand(screenIds, startPoints, screenGroupId);
450     if (screenGroupId == SCREEN_ID_INVALID) {
451         WLOGFI("Make expand failed");
452     }
453     return ret;
454 }
455 
MakeMirror(ScreenId mainScreenId,std::vector<ScreenId> mirrorScreenId,ScreenId & screenGroupId)456 DMError ScreenManager::MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenId, ScreenId& screenGroupId)
457 {
458     WLOGFI("Make mirror for screen: %{public}" PRIu64"", mainScreenId);
459     if (mirrorScreenId.size() > MAX_SCREEN_SIZE) {
460         WLOGFW("Make Mirror failed. The mirrorScreenId size is bigger than %{public}u.", MAX_SCREEN_SIZE);
461         return DMError::DM_ERROR_INVALID_PARAM;
462     }
463     DMError ret = SingletonContainer::Get<ScreenManagerAdapter>().MakeMirror(mainScreenId, mirrorScreenId,
464                                                                              screenGroupId);
465     if (screenGroupId == SCREEN_ID_INVALID) {
466         WLOGFI("create mirror failed");
467     }
468     return ret;
469 }
470 
StopExpand(const std::vector<ScreenId> & expandScreenIds)471 DMError ScreenManager::StopExpand(const std::vector<ScreenId>& expandScreenIds)
472 {
473     WLOGFI("Stop expand");
474     if (expandScreenIds.empty()) {
475         return DMError::DM_OK;
476     }
477     return SingletonContainer::Get<ScreenManagerAdapter>().StopExpand(expandScreenIds);
478 }
479 
StopMirror(const std::vector<ScreenId> & mirrorScreenIds)480 DMError ScreenManager::StopMirror(const std::vector<ScreenId>& mirrorScreenIds)
481 {
482     WLOGFI("Stop mirror");
483     if (mirrorScreenIds.empty()) {
484         return DMError::DM_OK;
485     }
486     return SingletonContainer::Get<ScreenManagerAdapter>().StopMirror(mirrorScreenIds);
487 }
488 
RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)489 DMError ScreenManager::RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)
490 {
491     WLOGFI("screens.size=%{public}llu", (unsigned long long)screens.size());
492     if (screens.empty()) {
493         WLOGFW("RemoveVirtualScreenFromGroup failed. screens is empty.");
494         return DMError::DM_ERROR_INVALID_PARAM;
495     }
496     if (screens.size() > MAX_SCREEN_SIZE) {
497         WLOGFW("RemoveVirtualScreenFromGroup failed. The screens size is bigger than %{public}u.", MAX_SCREEN_SIZE);
498         return DMError::DM_ERROR_INVALID_PARAM;
499     }
500     SingletonContainer::Get<ScreenManagerAdapter>().RemoveVirtualScreenFromGroup(screens);
501     return DMError::DM_OK;
502 }
503 
CreateVirtualScreen(VirtualScreenOption option)504 ScreenId ScreenManager::CreateVirtualScreen(VirtualScreenOption option)
505 {
506     return pImpl_->CreateVirtualScreen(option);
507 }
508 
CreateVirtualScreen(VirtualScreenOption option)509 ScreenId ScreenManager::Impl::CreateVirtualScreen(VirtualScreenOption option)
510 {
511     //  After the process creating the virtual screen is killed, DMS needs to delete the virtual screen
512     if (virtualScreenAgent_ == nullptr) {
513         virtualScreenAgent_ = new DisplayManagerAgentDefault();
514     }
515     return SingletonContainer::Get<ScreenManagerAdapter>().CreateVirtualScreen(option, virtualScreenAgent_);
516 }
517 
DestroyVirtualScreen(ScreenId screenId)518 DMError ScreenManager::DestroyVirtualScreen(ScreenId screenId)
519 {
520     return SingletonContainer::Get<ScreenManagerAdapter>().DestroyVirtualScreen(screenId);
521 }
522 
SetVirtualScreenSurface(ScreenId screenId,sptr<Surface> surface)523 DMError ScreenManager::SetVirtualScreenSurface(ScreenId screenId, sptr<Surface> surface)
524 {
525     return SingletonContainer::Get<ScreenManagerAdapter>().SetVirtualScreenSurface(screenId, surface);
526 }
527 
SetScreenPowerForAll(ScreenPowerState state,PowerStateChangeReason reason)528 bool ScreenManager::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)
529 {
530     WLOGFI("state:%{public}u, reason:%{public}u", state, reason);
531     return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenPowerForAll(state, reason);
532 }
533 
GetScreenPower(ScreenId dmsScreenId)534 ScreenPowerState ScreenManager::GetScreenPower(ScreenId dmsScreenId)
535 {
536     return SingletonContainer::Get<ScreenManagerAdapter>().GetScreenPower(dmsScreenId);
537 }
538 
SetScreenRotationLocked(bool isLocked)539 DMError ScreenManager::SetScreenRotationLocked(bool isLocked)
540 {
541     return SingletonContainer::Get<ScreenManagerAdapter>().SetScreenRotationLocked(isLocked);
542 }
543 
IsScreenRotationLocked(bool & isLocked)544 DMError ScreenManager::IsScreenRotationLocked(bool& isLocked)
545 {
546     return SingletonContainer::Get<ScreenManagerAdapter>().IsScreenRotationLocked(isLocked);
547 }
548 
NotifyScreenConnect(sptr<ScreenInfo> info)549 void ScreenManager::Impl::NotifyScreenConnect(sptr<ScreenInfo> info)
550 {
551     std::lock_guard<std::recursive_mutex> lock(mutex_);
552     UpdateScreenInfoLocked(info);
553 }
554 
NotifyScreenDisconnect(ScreenId screenId)555 void ScreenManager::Impl::NotifyScreenDisconnect(ScreenId screenId)
556 {
557     WLOGFI("screenId:%{public}" PRIu64".", screenId);
558     std::lock_guard<std::recursive_mutex> lock(mutex_);
559     screenMap_.erase(screenId);
560 }
561 
NotifyScreenChange(const sptr<ScreenInfo> & screenInfo)562 void ScreenManager::Impl::NotifyScreenChange(const sptr<ScreenInfo>& screenInfo)
563 {
564     std::lock_guard<std::recursive_mutex> lock(mutex_);
565     UpdateScreenInfoLocked(screenInfo);
566 }
567 
NotifyScreenChange(const std::vector<sptr<ScreenInfo>> & screenInfos)568 void ScreenManager::Impl::NotifyScreenChange(const std::vector<sptr<ScreenInfo>>& screenInfos)
569 {
570     std::lock_guard<std::recursive_mutex> lock(mutex_);
571     for (auto screenInfo : screenInfos) {
572         UpdateScreenInfoLocked(screenInfo);
573     }
574 }
575 
UpdateScreenInfoLocked(sptr<ScreenInfo> screenInfo)576 bool ScreenManager::Impl::UpdateScreenInfoLocked(sptr<ScreenInfo> screenInfo)
577 {
578     if (screenInfo == nullptr) {
579         WLOGFE("displayInfo is null");
580         return false;
581     }
582     ScreenId screenId = screenInfo->GetScreenId();
583     WLOGFI("screenId:%{public}" PRIu64".", screenId);
584     if (screenId == SCREEN_ID_INVALID) {
585         WLOGFE("displayId is invalid.");
586         return false;
587     }
588     auto iter = screenMap_.find(screenId);
589     if (iter != screenMap_.end() && iter->second != nullptr) {
590         WLOGFI("get screen in screen map");
591         iter->second->UpdateScreenInfo(screenInfo);
592         return true;
593     }
594     sptr<Screen> screen = new Screen(screenInfo);
595     screenMap_[screenId] = screen;
596     return true;
597 }
598 
isAllListenersRemoved() const599 bool ScreenManager::Impl::isAllListenersRemoved() const
600 {
601     return screenListeners_.empty() && screenGroupListeners_.empty() && virtualScreenGroupListeners_.empty();
602 }
603 
OnRemoteDied()604 void ScreenManager::Impl::OnRemoteDied()
605 {
606     WLOGFI("dms is died");
607     std::lock_guard<std::recursive_mutex> lock(mutex_);
608     screenManagerListener_ = nullptr;
609     virtualScreenAgent_ = nullptr;
610 }
611 
OnRemoteDied()612 void ScreenManager::OnRemoteDied()
613 {
614     pImpl_->OnRemoteDied();
615 }
616 } // namespace OHOS::Rosen