1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at.
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software,
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "display_manager_lite.h"
17 
18 #include <chrono>
19 #include <cinttypes>
20 
21 #include "display_manager_adapter_lite.h"
22 #include "display_manager_agent_default.h"
23 #include "dm_common.h"
24 #include "singleton_delegator.h"
25 #include "window_manager_hilog.h"
26 
27 namespace OHOS::Rosen {
28 namespace {
29 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerLite"};
30 }
31 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerLite)
32 
33 class DisplayManagerLite::Impl : public RefBase {
34 public:
Impl(std::recursive_mutex & mutex)35     Impl(std::recursive_mutex& mutex) : mutex_(mutex) {}
36     ~Impl();
37     static inline SingletonDelegator<DisplayManagerLite> delegator;
38     sptr<DisplayLite> GetDefaultDisplay();
39     FoldStatus GetFoldStatus();
40     FoldDisplayMode GetFoldDisplayMode();
41     void SetFoldDisplayMode(const FoldDisplayMode);
42     bool IsFoldable();
43 
44     DMError RegisterDisplayListener(sptr<IDisplayListener> listener);
45     DMError UnregisterDisplayListener(sptr<IDisplayListener> listener);
46     DMError RegisterFoldStatusListener(sptr<IFoldStatusListener> listener);
47     DMError UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener);
48     DMError RegisterDisplayModeListener(sptr<IDisplayModeListener> listener);
49     DMError UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener);
50     void OnRemoteDied();
51     sptr<DisplayLite> GetDisplayById(DisplayId displayId);
52     /*
53      * used by powermgr
54      */
55     bool SetDisplayState(DisplayState state, DisplayStateCallback callback);
56 private:
57     void NotifyDisplayCreate(sptr<DisplayInfo> info);
58     void NotifyDisplayDestroy(DisplayId);
59     void NotifyDisplayChange(sptr<DisplayInfo> displayInfo);
60     bool UpdateDisplayInfoLocked(sptr<DisplayInfo>);
61     void NotifyFoldStatusChanged(FoldStatus foldStatus);
62     void NotifyDisplayModeChanged(FoldDisplayMode displayMode);
63     /*
64      * used by powermgr
65      */
66     void NotifyDisplayStateChanged(DisplayId id, DisplayState state);
67     void ClearDisplayStateCallback();
68     void Clear();
69 
70     std::map<DisplayId, sptr<DisplayLite>> displayMap_;
71     DisplayStateCallback displayStateCallback_;
72     std::recursive_mutex& mutex_;
73     std::set<sptr<IDisplayListener>> displayListeners_;
74     std::set<sptr<IFoldStatusListener>> foldStatusListeners_;
75     std::set<sptr<IDisplayModeListener>> displayModeListeners_;
76     class DisplayManagerListener;
77     sptr<DisplayManagerListener> displayManagerListener_;
78     class DisplayManagerFoldStatusAgent;
79     sptr<DisplayManagerFoldStatusAgent> foldStatusListenerAgent_;
80     class DisplayManagerDisplayModeAgent;
81     sptr<DisplayManagerDisplayModeAgent> displayModeListenerAgent_;
82     /*
83      * used by powermgr
84      */
85     class DisplayManagerAgent;
86     sptr<DisplayManagerAgent> displayStateAgent_;
87 };
88 
89 class DisplayManagerLite::Impl::DisplayManagerListener : public DisplayManagerAgentDefault {
90 public:
DisplayManagerListener(sptr<Impl> impl)91     explicit DisplayManagerListener(sptr<Impl> impl) : pImpl_(impl)
92     {
93     }
94 
OnDisplayCreate(sptr<DisplayInfo> displayInfo)95     void OnDisplayCreate(sptr<DisplayInfo> displayInfo) override
96     {
97         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
98             WLOGFE("onDisplayCreate: displayInfo is nullptr");
99             return;
100         }
101         if (pImpl_ == nullptr) {
102             WLOGFE("onDisplayCreate: pImpl_ is nullptr");
103             return;
104         }
105         pImpl_->NotifyDisplayCreate(displayInfo);
106         std::set<sptr<IDisplayListener>> displayListeners;
107         {
108             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
109             displayListeners = pImpl_->displayListeners_;
110         }
111         for (auto listener : displayListeners) {
112             listener->OnCreate(displayInfo->GetDisplayId());
113         }
114     };
115 
OnDisplayDestroy(DisplayId displayId)116     void OnDisplayDestroy(DisplayId displayId) override
117     {
118         if (displayId == DISPLAY_ID_INVALID) {
119             WLOGFE("onDisplayDestroy: displayId is invalid");
120             return;
121         }
122         if (pImpl_ == nullptr) {
123             WLOGFE("onDisplayDestroy: impl is nullptr");
124             return;
125         }
126         pImpl_->NotifyDisplayDestroy(displayId);
127         std::set<sptr<IDisplayListener>> displayListeners;
128         {
129             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
130             displayListeners = pImpl_->displayListeners_;
131         }
132         for (auto listener : displayListeners) {
133             listener->OnDestroy(displayId);
134         }
135     };
136 
OnDisplayChange(sptr<DisplayInfo> displayInfo,DisplayChangeEvent event)137     void OnDisplayChange(sptr<DisplayInfo> displayInfo, DisplayChangeEvent event) override
138     {
139         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
140             WLOGFE("onDisplayChange: displayInfo is nullptr");
141             return;
142         }
143         if (pImpl_ == nullptr) {
144             WLOGFE("onDisplayChange: pImpl_ is nullptr");
145             return;
146         }
147         WLOGD("onDisplayChange: display %{public}" PRIu64", event %{public}u", displayInfo->GetDisplayId(), event);
148         pImpl_->NotifyDisplayChange(displayInfo);
149         std::set<sptr<IDisplayListener>> displayListeners;
150         {
151             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
152             displayListeners = pImpl_->displayListeners_;
153         }
154         for (auto listener : displayListeners) {
155             listener->OnChange(displayInfo->GetDisplayId());
156         }
157     };
158 private:
159     sptr<Impl> pImpl_;
160 };
161 
162 class DisplayManagerLite::Impl::DisplayManagerFoldStatusAgent : public DisplayManagerAgentDefault {
163 public:
DisplayManagerFoldStatusAgent(sptr<Impl> impl)164     explicit DisplayManagerFoldStatusAgent(sptr<Impl> impl) : pImpl_(impl)
165     {
166     }
167     ~DisplayManagerFoldStatusAgent() = default;
168 
NotifyFoldStatusChanged(FoldStatus foldStatus)169     virtual void NotifyFoldStatusChanged(FoldStatus foldStatus) override
170     {
171         pImpl_->NotifyFoldStatusChanged(foldStatus);
172     }
173 private:
174     sptr<Impl> pImpl_;
175 };
176 
177 class DisplayManagerLite::Impl::DisplayManagerDisplayModeAgent : public DisplayManagerAgentDefault {
178 public:
DisplayManagerDisplayModeAgent(sptr<Impl> impl)179     explicit DisplayManagerDisplayModeAgent(sptr<Impl> impl) : pImpl_(impl)
180     {
181     }
182     ~DisplayManagerDisplayModeAgent() = default;
183 
NotifyDisplayModeChanged(FoldDisplayMode displayMode)184     virtual void NotifyDisplayModeChanged(FoldDisplayMode displayMode) override
185     {
186         pImpl_->NotifyDisplayModeChanged(displayMode);
187     }
188 private:
189     sptr<Impl> pImpl_;
190 };
191 
192 /*
193  * used by powermgr
194  */
195 class DisplayManagerLite::Impl::DisplayManagerAgent : public DisplayManagerAgentDefault {
196 public:
DisplayManagerAgent(sptr<Impl> impl)197     explicit DisplayManagerAgent(sptr<Impl> impl) : pImpl_(impl)
198     {
199     }
200     ~DisplayManagerAgent() = default;
201 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)202     virtual void NotifyDisplayStateChanged(DisplayId id, DisplayState state) override
203     {
204         pImpl_->NotifyDisplayStateChanged(id, state);
205     }
206 private:
207     sptr<Impl> pImpl_;
208 };
209 
Clear()210 void DisplayManagerLite::Impl::Clear()
211 {
212     std::lock_guard<std::recursive_mutex> lock(mutex_);
213     DMError res = DMError::DM_OK;
214     if (displayManagerListener_ != nullptr) {
215         res = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
216             displayManagerListener_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
217     }
218     displayManagerListener_ = nullptr;
219     if (res != DMError::DM_OK) {
220         WLOGFW("UnregisterDisplayManagerAgent DISPLAY_EVENT_LISTENER failed");
221     }
222     ClearDisplayStateCallback();
223 }
224 
~Impl()225 DisplayManagerLite::Impl::~Impl()
226 {
227     Clear();
228 }
229 
DisplayManagerLite()230 DisplayManagerLite::DisplayManagerLite() : pImpl_(new Impl(mutex_))
231 {
232 }
233 
~DisplayManagerLite()234 DisplayManagerLite::~DisplayManagerLite()
235 {
236     std::lock_guard<std::recursive_mutex> lock(mutex_);
237     destroyed_ = true;
238 }
239 
RegisterDisplayListener(sptr<IDisplayListener> listener)240 DMError DisplayManagerLite::Impl::RegisterDisplayListener(sptr<IDisplayListener> listener)
241 {
242     std::lock_guard<std::recursive_mutex> lock(mutex_);
243     DMError ret = DMError::DM_OK;
244     if (displayManagerListener_ == nullptr) {
245         displayManagerListener_ = new DisplayManagerListener(this);
246         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
247             displayManagerListener_,
248             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
249     }
250     if (ret != DMError::DM_OK) {
251         WLOGFW("RegisterDisplayManagerAgent failed");
252         displayManagerListener_ = nullptr;
253     } else {
254         displayListeners_.insert(listener);
255     }
256     return ret;
257 }
258 
RegisterDisplayListener(sptr<IDisplayListener> listener)259 DMError DisplayManagerLite::RegisterDisplayListener(sptr<IDisplayListener> listener)
260 {
261     if (listener == nullptr) {
262         WLOGFE("RegisterDisplayListener listener is nullptr");
263         return DMError::DM_ERROR_NULLPTR;
264     }
265     return pImpl_->RegisterDisplayListener(listener);
266 }
267 
UnregisterDisplayListener(sptr<IDisplayListener> listener)268 DMError DisplayManagerLite::Impl::UnregisterDisplayListener(sptr<IDisplayListener> listener)
269 {
270     std::lock_guard<std::recursive_mutex> lock(mutex_);
271     auto iter = std::find(displayListeners_.begin(), displayListeners_.end(), listener);
272     if (iter == displayListeners_.end()) {
273         WLOGFE("could not find this listener");
274         return DMError::DM_ERROR_NULLPTR;
275     }
276     displayListeners_.erase(iter);
277     DMError ret = DMError::DM_OK;
278     if (displayListeners_.empty() && displayManagerListener_ != nullptr) {
279         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
280             displayManagerListener_,
281             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
282         displayManagerListener_ = nullptr;
283     }
284     return ret;
285 }
286 
UnregisterDisplayListener(sptr<IDisplayListener> listener)287 DMError DisplayManagerLite::UnregisterDisplayListener(sptr<IDisplayListener> listener)
288 {
289     if (listener == nullptr) {
290         WLOGFE("UnregisterDisplayListener listener is nullptr");
291         return DMError::DM_ERROR_NULLPTR;
292     }
293     return pImpl_->UnregisterDisplayListener(listener);
294 }
295 
NotifyDisplayCreate(sptr<DisplayInfo> info)296 void DisplayManagerLite::Impl::NotifyDisplayCreate(sptr<DisplayInfo> info)
297 {
298     std::lock_guard<std::recursive_mutex> lock(mutex_);
299     UpdateDisplayInfoLocked(info);
300 }
301 
NotifyDisplayDestroy(DisplayId displayId)302 void DisplayManagerLite::Impl::NotifyDisplayDestroy(DisplayId displayId)
303 {
304     WLOGFD("displayId:%{public}" PRIu64".", displayId);
305     std::lock_guard<std::recursive_mutex> lock(mutex_);
306     displayMap_.erase(displayId);
307 }
308 
NotifyDisplayChange(sptr<DisplayInfo> displayInfo)309 void DisplayManagerLite::Impl::NotifyDisplayChange(sptr<DisplayInfo> displayInfo)
310 {
311     std::lock_guard<std::recursive_mutex> lock(mutex_);
312     UpdateDisplayInfoLocked(displayInfo);
313 }
314 
UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)315 bool DisplayManagerLite::Impl::UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)
316 {
317     if (displayInfo == nullptr) {
318         WLOGFE("displayInfo is null");
319         return false;
320     }
321     DisplayId displayId = displayInfo->GetDisplayId();
322     WLOGFD("displayId:%{public}" PRIu64".", displayId);
323     if (displayId == DISPLAY_ID_INVALID) {
324         WLOGFE("displayId is invalid");
325         return false;
326     }
327     auto iter = displayMap_.find(displayId);
328     if (iter != displayMap_.end() && iter->second != nullptr) {
329         WLOGFD("get screen in screen map");
330         iter->second->UpdateDisplayInfo(displayInfo);
331         return true;
332     }
333     sptr<DisplayLite> display = new DisplayLite("", displayInfo);
334     displayMap_[displayId] = display;
335     return true;
336 }
337 
RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)338 DMError DisplayManagerLite::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
339 {
340     if (listener == nullptr) {
341         WLOGFE("IFoldStatusListener listener is nullptr.");
342         return DMError::DM_ERROR_NULLPTR;
343     }
344     return pImpl_->RegisterFoldStatusListener(listener);
345 }
346 
RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)347 DMError DisplayManagerLite::Impl::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
348 {
349     std::lock_guard<std::recursive_mutex> lock(mutex_);
350     DMError ret = DMError::DM_OK;
351     if (foldStatusListenerAgent_ == nullptr) {
352         foldStatusListenerAgent_ = new DisplayManagerFoldStatusAgent(this);
353         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
354             foldStatusListenerAgent_,
355             DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
356     }
357     if (ret != DMError::DM_OK) {
358         WLOGFW("RegisterFoldStatusListener failed !");
359         foldStatusListenerAgent_ = nullptr;
360     } else {
361         WLOGI("IFoldStatusListener register success");
362         foldStatusListeners_.insert(listener);
363     }
364     return ret;
365 }
366 
UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)367 DMError DisplayManagerLite::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
368 {
369     if (listener == nullptr) {
370         WLOGFE("UnregisterFoldStatusListener listener is nullptr.");
371         return DMError::DM_ERROR_NULLPTR;
372     }
373     return pImpl_->UnregisterFoldStatusListener(listener);
374 }
375 
UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)376 DMError DisplayManagerLite::Impl::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
377 {
378     std::lock_guard<std::recursive_mutex> lock(mutex_);
379     auto iter = std::find(foldStatusListeners_.begin(), foldStatusListeners_.end(), listener);
380     if (iter == foldStatusListeners_.end()) {
381         WLOGFE("could not find this listener");
382         return DMError::DM_ERROR_NULLPTR;
383     }
384     foldStatusListeners_.erase(iter);
385     DMError ret = DMError::DM_OK;
386     if (foldStatusListeners_.empty() && foldStatusListenerAgent_ != nullptr) {
387         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
388             foldStatusListenerAgent_,
389             DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
390         foldStatusListenerAgent_ = nullptr;
391     }
392     return ret;
393 }
394 
NotifyFoldStatusChanged(FoldStatus foldStatus)395 void DisplayManagerLite::Impl::NotifyFoldStatusChanged(FoldStatus foldStatus)
396 {
397     std::set<sptr<IFoldStatusListener>> foldStatusListeners;
398     {
399         std::lock_guard<std::recursive_mutex> lock(mutex_);
400         foldStatusListeners = foldStatusListeners_;
401     }
402     for (auto& listener : foldStatusListeners) {
403         listener->OnFoldStatusChanged(foldStatus);
404     }
405 }
406 
RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)407 DMError DisplayManagerLite::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
408 {
409     if (listener == nullptr) {
410         WLOGFE("IDisplayModeListener listener is nullptr.");
411         return DMError::DM_ERROR_NULLPTR;
412     }
413     return pImpl_->RegisterDisplayModeListener(listener);
414 }
415 
RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)416 DMError DisplayManagerLite::Impl::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
417 {
418     std::lock_guard<std::recursive_mutex> lock(mutex_);
419     DMError ret = DMError::DM_OK;
420     if (displayModeListenerAgent_ == nullptr) {
421         displayModeListenerAgent_ = new DisplayManagerDisplayModeAgent(this);
422         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
423             displayModeListenerAgent_,
424             DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
425     }
426     if (ret != DMError::DM_OK) {
427         WLOGFW("RegisterDisplayModeListener failed !");
428         displayModeListenerAgent_ = nullptr;
429     } else {
430         WLOGI("IDisplayModeListener register success");
431         displayModeListeners_.insert(listener);
432     }
433     return ret;
434 }
435 
UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)436 DMError DisplayManagerLite::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
437 {
438     if (listener == nullptr) {
439         WLOGFE("UnregisterDisplayModeListener listener is nullptr.");
440         return DMError::DM_ERROR_NULLPTR;
441     }
442     return pImpl_->UnregisterDisplayModeListener(listener);
443 }
444 
UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)445 DMError DisplayManagerLite::Impl::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
446 {
447     std::lock_guard<std::recursive_mutex> lock(mutex_);
448     auto iter = std::find(displayModeListeners_.begin(), displayModeListeners_.end(), listener);
449     if (iter == displayModeListeners_.end()) {
450         WLOGFE("could not find this listener");
451         return DMError::DM_ERROR_NULLPTR;
452     }
453     displayModeListeners_.erase(iter);
454     DMError ret = DMError::DM_OK;
455     if (displayModeListeners_.empty() && displayModeListenerAgent_ != nullptr) {
456         ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
457             displayModeListenerAgent_,
458             DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
459         displayModeListenerAgent_ = nullptr;
460     }
461     return ret;
462 }
463 
NotifyDisplayModeChanged(FoldDisplayMode displayMode)464 void DisplayManagerLite::Impl::NotifyDisplayModeChanged(FoldDisplayMode displayMode)
465 {
466     std::set<sptr<IDisplayModeListener>> displayModeListeners;
467     {
468         std::lock_guard<std::recursive_mutex> lock(mutex_);
469         displayModeListeners = displayModeListeners_;
470     }
471     for (auto& listener : displayModeListeners) {
472         listener->OnDisplayModeChanged(displayMode);
473     }
474 }
475 
GetFoldStatus()476 FoldStatus DisplayManagerLite::GetFoldStatus()
477 {
478     return pImpl_->GetFoldStatus();
479 }
480 
GetFoldStatus()481 FoldStatus DisplayManagerLite::Impl::GetFoldStatus()
482 {
483     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldStatus();
484 }
485 
GetDefaultDisplay()486 sptr<DisplayLite> DisplayManagerLite::GetDefaultDisplay()
487 {
488     return pImpl_->GetDefaultDisplay();
489 }
490 
GetDefaultDisplay()491 sptr<DisplayLite> DisplayManagerLite::Impl::GetDefaultDisplay()
492 {
493     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDefaultDisplayInfo();
494     if (displayInfo == nullptr) {
495         return nullptr;
496     }
497     auto displayId = displayInfo->GetDisplayId();
498     std::lock_guard<std::recursive_mutex> lock(mutex_);
499     if (!UpdateDisplayInfoLocked(displayInfo)) {
500         displayMap_.erase(displayId);
501         return nullptr;
502     }
503     return displayMap_[displayId];
504 }
505 
IsFoldable()506 bool DisplayManagerLite::IsFoldable()
507 {
508     return pImpl_->IsFoldable();
509 }
510 
IsFoldable()511 bool DisplayManagerLite::Impl::IsFoldable()
512 {
513     return SingletonContainer::Get<DisplayManagerAdapterLite>().IsFoldable();
514 }
515 
GetFoldDisplayMode()516 FoldDisplayMode DisplayManagerLite::GetFoldDisplayMode()
517 {
518     return pImpl_->GetFoldDisplayMode();
519 }
520 
GetFoldDisplayMode()521 FoldDisplayMode DisplayManagerLite::Impl::GetFoldDisplayMode()
522 {
523     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldDisplayMode();
524 }
525 
SetFoldDisplayMode(const FoldDisplayMode mode)526 void DisplayManagerLite::SetFoldDisplayMode(const FoldDisplayMode mode)
527 {
528     return pImpl_->SetFoldDisplayMode(mode);
529 }
530 
SetFoldDisplayMode(const FoldDisplayMode mode)531 void DisplayManagerLite::Impl::SetFoldDisplayMode(const FoldDisplayMode mode)
532 {
533     return SingletonContainer::Get<DisplayManagerAdapterLite>().SetFoldDisplayMode(mode);
534 }
535 
OnRemoteDied()536 void DisplayManagerLite::Impl::OnRemoteDied()
537 {
538     WLOGFI("dms is died");
539     std::lock_guard<std::recursive_mutex> lock(mutex_);
540     displayManagerListener_ = nullptr;
541 }
542 
OnRemoteDied()543 void DisplayManagerLite::OnRemoteDied()
544 {
545     pImpl_->OnRemoteDied();
546 }
547 
GetDisplayById(DisplayId displayId)548 sptr<DisplayLite> DisplayManagerLite::Impl::GetDisplayById(DisplayId displayId)
549 {
550     WLOGFD("GetDisplayById start, displayId: %{public}" PRIu64" ", displayId);
551     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDisplayInfo(displayId);
552     std::lock_guard<std::recursive_mutex> lock(mutex_);
553     if (!UpdateDisplayInfoLocked(displayInfo)) {
554         displayMap_.erase(displayId);
555         return nullptr;
556     }
557     return displayMap_[displayId];
558 }
559 
GetDisplayById(DisplayId displayId)560 sptr<DisplayLite> DisplayManagerLite::GetDisplayById(DisplayId displayId)
561 {
562     if (destroyed_) {
563         return nullptr;
564     }
565     std::lock_guard<std::recursive_mutex> lock(mutex_);
566     return pImpl_->GetDisplayById(displayId);
567 }
568 
569 /*
570  * used by powermgr
571  */
WakeUpBegin(PowerStateChangeReason reason)572 bool DisplayManagerLite::WakeUpBegin(PowerStateChangeReason reason)
573 {
574     WLOGFD("[UL_POWER]WakeUpBegin start, reason:%{public}u", reason);
575     return SingletonContainer::Get<DisplayManagerAdapterLite>().WakeUpBegin(reason);
576 }
577 
WakeUpEnd()578 bool DisplayManagerLite::WakeUpEnd()
579 {
580     WLOGFD("[UL_POWER]WakeUpEnd start");
581     return SingletonContainer::Get<DisplayManagerAdapterLite>().WakeUpEnd();
582 }
583 
SuspendBegin(PowerStateChangeReason reason)584 bool DisplayManagerLite::SuspendBegin(PowerStateChangeReason reason)
585 {
586     // dms->wms notify other windows to hide
587     WLOGFD("[UL_POWER]SuspendBegin start, reason:%{public}u", reason);
588     return SingletonContainer::Get<DisplayManagerAdapterLite>().SuspendBegin(reason);
589 }
590 
SuspendEnd()591 bool DisplayManagerLite::SuspendEnd()
592 {
593     WLOGFD("[UL_POWER]SuspendEnd start");
594     return SingletonContainer::Get<DisplayManagerAdapterLite>().SuspendEnd();
595 }
596 
SetDisplayState(DisplayState state,DisplayStateCallback callback)597 bool DisplayManagerLite::SetDisplayState(DisplayState state, DisplayStateCallback callback)
598 {
599     return pImpl_->SetDisplayState(state, callback);
600 }
601 
GetDisplayState(DisplayId displayId)602 DisplayState DisplayManagerLite::GetDisplayState(DisplayId displayId)
603 {
604     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetDisplayState(displayId);
605 }
606 
SetDisplayState(DisplayState state,DisplayStateCallback callback)607 bool DisplayManagerLite::Impl::SetDisplayState(DisplayState state, DisplayStateCallback callback)
608 {
609     WLOGFD("[UL_POWER]state:%{public}u", state);
610     bool ret = true;
611     {
612         std::lock_guard<std::recursive_mutex> lock(mutex_);
613         if (displayStateCallback_ != nullptr || callback == nullptr) {
614             WLOGFI("[UL_POWER]previous callback not called or callback invalid");
615             if (displayStateCallback_ != nullptr) {
616                 WLOGFI("[UL_POWER]previous callback not called, the displayStateCallback_ is not null");
617             }
618             if (callback == nullptr) {
619                 WLOGFI("[UL_POWER]Invalid callback received");
620             }
621             return false;
622         }
623         displayStateCallback_ = callback;
624 
625         if (displayStateAgent_ == nullptr) {
626             displayStateAgent_ = new DisplayManagerAgent(this);
627             ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
628                 displayStateAgent_,
629                 DisplayManagerAgentType::DISPLAY_STATE_LISTENER) == DMError::DM_OK;
630         }
631     }
632     ret = ret && SingletonContainer::Get<DisplayManagerAdapterLite>().SetDisplayState(state);
633     if (!ret) {
634         ClearDisplayStateCallback();
635     }
636     return ret;
637 }
638 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)639 void DisplayManagerLite::Impl::NotifyDisplayStateChanged(DisplayId id, DisplayState state)
640 {
641     WLOGFD("state:%{public}u", state);
642     DisplayStateCallback displayStateCallback = nullptr;
643     {
644         std::lock_guard<std::recursive_mutex> lock(mutex_);
645         displayStateCallback = displayStateCallback_;
646     }
647     if (displayStateCallback) {
648         displayStateCallback(state);
649         ClearDisplayStateCallback();
650         return;
651     }
652     WLOGFW("callback_ target is not set!");
653 }
654 
ClearDisplayStateCallback()655 void DisplayManagerLite::Impl::ClearDisplayStateCallback()
656 {
657     std::lock_guard<std::recursive_mutex> lock(mutex_);
658     WLOGFD("[UL_POWER]Clear displaystatecallback enter");
659     displayStateCallback_ = nullptr;
660     if (displayStateAgent_ != nullptr) {
661         WLOGFI("[UL_POWER]UnregisterDisplayManagerAgent enter and displayStateAgent_ is cleared");
662         SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(displayStateAgent_,
663             DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
664         displayStateAgent_ = nullptr;
665     }
666 }
667 
TryToCancelScreenOff()668 bool DisplayManagerLite::TryToCancelScreenOff()
669 {
670     WLOGFD("[UL_POWER]TryToCancelScreenOff start");
671     return SingletonContainer::Get<DisplayManagerAdapterLite>().TryToCancelScreenOff();
672 }
673 
SetScreenBrightness(uint64_t screenId,uint32_t level)674 bool DisplayManagerLite::SetScreenBrightness(uint64_t screenId, uint32_t level)
675 {
676     WLOGFD("[UL_POWER]SetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
677     SingletonContainer::Get<DisplayManagerAdapterLite>().SetScreenBrightness(screenId, level);
678     return true;
679 }
680 
GetScreenBrightness(uint64_t screenId) const681 uint32_t DisplayManagerLite::GetScreenBrightness(uint64_t screenId) const
682 {
683     uint32_t level = SingletonContainer::Get<DisplayManagerAdapterLite>().GetScreenBrightness(screenId);
684     WLOGFD("[UL_POWER]GetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
685     return level;
686 }
687 
GetDefaultDisplayId()688 DisplayId DisplayManagerLite::GetDefaultDisplayId()
689 {
690     auto info = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDefaultDisplayInfo();
691     if (info == nullptr) {
692         return DISPLAY_ID_INVALID;
693     }
694     return info->GetDisplayId();
695 }
696 
GetAllDisplayIds()697 std::vector<DisplayId> DisplayManagerLite::GetAllDisplayIds()
698 {
699     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetAllDisplayIds();
700 }
701 
GetVirtualScreenFlag(ScreenId screenId)702 VirtualScreenFlag DisplayManagerLite::GetVirtualScreenFlag(ScreenId screenId)
703 {
704     return SingletonContainer::Get<DisplayManagerAdapterLite>().GetVirtualScreenFlag(screenId);
705 }
706 
707 } // namespace OHOS::Rosen