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