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
27namespace OHOS::Rosen {
28namespace {
29constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerLite"};
30}
31WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerLite)
32
33class DisplayManagerLite::Impl : public RefBase {
34public:
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);
56private:
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
89class DisplayManagerLite::Impl::DisplayManagerListener : public DisplayManagerAgentDefault {
90public:
91    explicit DisplayManagerListener(sptr<Impl> impl) : pImpl_(impl)
92    {
93    }
94
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
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
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::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
150        for (auto listener : pImpl_->displayListeners_) {
151            listener->OnChange(displayInfo->GetDisplayId());
152        }
153    };
154private:
155    sptr<Impl> pImpl_;
156};
157
158class DisplayManagerLite::Impl::DisplayManagerFoldStatusAgent : public DisplayManagerAgentDefault {
159public:
160    explicit DisplayManagerFoldStatusAgent(sptr<Impl> impl) : pImpl_(impl)
161    {
162    }
163    ~DisplayManagerFoldStatusAgent() = default;
164
165    virtual void NotifyFoldStatusChanged(FoldStatus foldStatus) override
166    {
167        pImpl_->NotifyFoldStatusChanged(foldStatus);
168    }
169private:
170    sptr<Impl> pImpl_;
171};
172
173class DisplayManagerLite::Impl::DisplayManagerDisplayModeAgent : public DisplayManagerAgentDefault {
174public:
175    explicit DisplayManagerDisplayModeAgent(sptr<Impl> impl) : pImpl_(impl)
176    {
177    }
178    ~DisplayManagerDisplayModeAgent() = default;
179
180    virtual void NotifyDisplayModeChanged(FoldDisplayMode displayMode) override
181    {
182        pImpl_->NotifyDisplayModeChanged(displayMode);
183    }
184private:
185    sptr<Impl> pImpl_;
186};
187
188/*
189 * used by powermgr
190 */
191class DisplayManagerLite::Impl::DisplayManagerAgent : public DisplayManagerAgentDefault {
192public:
193    explicit DisplayManagerAgent(sptr<Impl> impl) : pImpl_(impl)
194    {
195    }
196    ~DisplayManagerAgent() = default;
197
198    virtual void NotifyDisplayStateChanged(DisplayId id, DisplayState state) override
199    {
200        pImpl_->NotifyDisplayStateChanged(id, state);
201    }
202private:
203    sptr<Impl> pImpl_;
204};
205
206void DisplayManagerLite::Impl::Clear()
207{
208    std::lock_guard<std::recursive_mutex> lock(mutex_);
209    DMError res = DMError::DM_OK;
210    if (displayManagerListener_ != nullptr) {
211        res = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
212            displayManagerListener_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
213    }
214    displayManagerListener_ = nullptr;
215    if (res != DMError::DM_OK) {
216        WLOGFW("UnregisterDisplayManagerAgent DISPLAY_EVENT_LISTENER failed");
217    }
218    ClearDisplayStateCallback();
219}
220
221DisplayManagerLite::Impl::~Impl()
222{
223    Clear();
224}
225
226DisplayManagerLite::DisplayManagerLite() : pImpl_(new Impl(mutex_))
227{
228}
229
230DisplayManagerLite::~DisplayManagerLite()
231{
232    std::lock_guard<std::recursive_mutex> lock(mutex_);
233    destroyed_ = true;
234}
235
236DMError DisplayManagerLite::Impl::RegisterDisplayListener(sptr<IDisplayListener> listener)
237{
238    std::lock_guard<std::recursive_mutex> lock(mutex_);
239    DMError ret = DMError::DM_OK;
240    if (displayManagerListener_ == nullptr) {
241        displayManagerListener_ = new DisplayManagerListener(this);
242        ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
243            displayManagerListener_,
244            DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
245    }
246    if (ret != DMError::DM_OK) {
247        WLOGFW("RegisterDisplayManagerAgent failed");
248        displayManagerListener_ = nullptr;
249    } else {
250        displayListeners_.insert(listener);
251    }
252    return ret;
253}
254
255DMError DisplayManagerLite::RegisterDisplayListener(sptr<IDisplayListener> listener)
256{
257    if (listener == nullptr) {
258        WLOGFE("RegisterDisplayListener listener is nullptr");
259        return DMError::DM_ERROR_NULLPTR;
260    }
261    return pImpl_->RegisterDisplayListener(listener);
262}
263
264DMError DisplayManagerLite::Impl::UnregisterDisplayListener(sptr<IDisplayListener> listener)
265{
266    std::lock_guard<std::recursive_mutex> lock(mutex_);
267    auto iter = std::find(displayListeners_.begin(), displayListeners_.end(), listener);
268    if (iter == displayListeners_.end()) {
269        WLOGFE("could not find this listener");
270        return DMError::DM_ERROR_NULLPTR;
271    }
272    displayListeners_.erase(iter);
273    DMError ret = DMError::DM_OK;
274    if (displayListeners_.empty() && displayManagerListener_ != nullptr) {
275        ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
276            displayManagerListener_,
277            DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
278        displayManagerListener_ = nullptr;
279    }
280    return ret;
281}
282
283DMError DisplayManagerLite::UnregisterDisplayListener(sptr<IDisplayListener> listener)
284{
285    if (listener == nullptr) {
286        WLOGFE("UnregisterDisplayListener listener is nullptr");
287        return DMError::DM_ERROR_NULLPTR;
288    }
289    return pImpl_->UnregisterDisplayListener(listener);
290}
291
292void DisplayManagerLite::Impl::NotifyDisplayCreate(sptr<DisplayInfo> info)
293{
294    std::lock_guard<std::recursive_mutex> lock(mutex_);
295    static_cast<void>(UpdateDisplayInfoLocked(info));
296}
297
298void DisplayManagerLite::Impl::NotifyDisplayDestroy(DisplayId displayId)
299{
300    WLOGFD("displayId:%{public}" PRIu64".", displayId);
301    std::lock_guard<std::recursive_mutex> lock(mutex_);
302    displayMap_.erase(displayId);
303}
304
305void DisplayManagerLite::Impl::NotifyDisplayChange(sptr<DisplayInfo> displayInfo)
306{
307    std::lock_guard<std::recursive_mutex> lock(mutex_);
308    static_cast<void>(UpdateDisplayInfoLocked(displayInfo));
309}
310
311bool DisplayManagerLite::Impl::UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)
312{
313    if (displayInfo == nullptr) {
314        WLOGFE("displayInfo is null");
315        return false;
316    }
317    DisplayId displayId = displayInfo->GetDisplayId();
318    WLOGFD("displayId:%{public}" PRIu64".", displayId);
319    if (displayId == DISPLAY_ID_INVALID) {
320        WLOGFE("displayId is invalid");
321        return false;
322    }
323    auto iter = displayMap_.find(displayId);
324    if (iter != displayMap_.end() && iter->second != nullptr) {
325        WLOGFD("get screen in screen map");
326        iter->second->UpdateDisplayInfo(displayInfo);
327        return true;
328    }
329    sptr<DisplayLite> display = new DisplayLite("", displayInfo);
330    displayMap_[displayId] = display;
331    return true;
332}
333
334DMError DisplayManagerLite::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
335{
336    if (listener == nullptr) {
337        WLOGFE("IFoldStatusListener listener is nullptr.");
338        return DMError::DM_ERROR_NULLPTR;
339    }
340    return pImpl_->RegisterFoldStatusListener(listener);
341}
342
343DMError DisplayManagerLite::Impl::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
344{
345    std::lock_guard<std::recursive_mutex> lock(mutex_);
346    DMError ret = DMError::DM_OK;
347    if (foldStatusListenerAgent_ == nullptr) {
348        foldStatusListenerAgent_ = new DisplayManagerFoldStatusAgent(this);
349        ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
350            foldStatusListenerAgent_,
351            DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
352    }
353    if (ret != DMError::DM_OK) {
354        WLOGFW("RegisterFoldStatusListener failed !");
355        foldStatusListenerAgent_ = nullptr;
356    } else {
357        WLOGI("IFoldStatusListener register success");
358        foldStatusListeners_.insert(listener);
359    }
360    return ret;
361}
362
363DMError DisplayManagerLite::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
364{
365    if (listener == nullptr) {
366        WLOGFE("UnregisterFoldStatusListener listener is nullptr.");
367        return DMError::DM_ERROR_NULLPTR;
368    }
369    return pImpl_->UnregisterFoldStatusListener(listener);
370}
371
372DMError DisplayManagerLite::Impl::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
373{
374    std::lock_guard<std::recursive_mutex> lock(mutex_);
375    auto iter = std::find(foldStatusListeners_.begin(), foldStatusListeners_.end(), listener);
376    if (iter == foldStatusListeners_.end()) {
377        WLOGFE("could not find this listener");
378        return DMError::DM_ERROR_NULLPTR;
379    }
380    foldStatusListeners_.erase(iter);
381    DMError ret = DMError::DM_OK;
382    if (foldStatusListeners_.empty() && foldStatusListenerAgent_ != nullptr) {
383        ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
384            foldStatusListenerAgent_,
385            DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
386        foldStatusListenerAgent_ = nullptr;
387    }
388    return ret;
389}
390
391void DisplayManagerLite::Impl::NotifyFoldStatusChanged(FoldStatus foldStatus)
392{
393    std::set<sptr<IFoldStatusListener>> foldStatusListeners;
394    {
395        std::lock_guard<std::recursive_mutex> lock(mutex_);
396        foldStatusListeners = foldStatusListeners_;
397    }
398    for (auto& listener : foldStatusListeners) {
399        listener->OnFoldStatusChanged(foldStatus);
400    }
401}
402
403DMError DisplayManagerLite::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
404{
405    if (listener == nullptr) {
406        WLOGFE("IDisplayModeListener listener is nullptr.");
407        return DMError::DM_ERROR_NULLPTR;
408    }
409    return pImpl_->RegisterDisplayModeListener(listener);
410}
411
412DMError DisplayManagerLite::Impl::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
413{
414    std::lock_guard<std::recursive_mutex> lock(mutex_);
415    DMError ret = DMError::DM_OK;
416    if (displayModeListenerAgent_ == nullptr) {
417        displayModeListenerAgent_ = new DisplayManagerDisplayModeAgent(this);
418        ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
419            displayModeListenerAgent_,
420            DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
421    }
422    if (ret != DMError::DM_OK) {
423        WLOGFW("RegisterDisplayModeListener failed !");
424        displayModeListenerAgent_ = nullptr;
425    } else {
426        WLOGI("IDisplayModeListener register success");
427        displayModeListeners_.insert(listener);
428    }
429    return ret;
430}
431
432DMError DisplayManagerLite::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
433{
434    if (listener == nullptr) {
435        WLOGFE("UnregisterDisplayModeListener listener is nullptr.");
436        return DMError::DM_ERROR_NULLPTR;
437    }
438    return pImpl_->UnregisterDisplayModeListener(listener);
439}
440
441DMError DisplayManagerLite::Impl::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
442{
443    std::lock_guard<std::recursive_mutex> lock(mutex_);
444    auto iter = std::find(displayModeListeners_.begin(), displayModeListeners_.end(), listener);
445    if (iter == displayModeListeners_.end()) {
446        WLOGFE("could not find this listener");
447        return DMError::DM_ERROR_NULLPTR;
448    }
449    displayModeListeners_.erase(iter);
450    DMError ret = DMError::DM_OK;
451    if (displayModeListeners_.empty() && displayModeListenerAgent_ != nullptr) {
452        ret = SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(
453            displayModeListenerAgent_,
454            DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
455        displayModeListenerAgent_ = nullptr;
456    }
457    return ret;
458}
459
460void DisplayManagerLite::Impl::NotifyDisplayModeChanged(FoldDisplayMode displayMode)
461{
462    std::set<sptr<IDisplayModeListener>> displayModeListeners;
463    {
464        std::lock_guard<std::recursive_mutex> lock(mutex_);
465        displayModeListeners = displayModeListeners_;
466    }
467    for (auto& listener : displayModeListeners) {
468        listener->OnDisplayModeChanged(displayMode);
469    }
470}
471
472FoldStatus DisplayManagerLite::GetFoldStatus()
473{
474    return pImpl_->GetFoldStatus();
475}
476
477FoldStatus DisplayManagerLite::Impl::GetFoldStatus()
478{
479    return SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldStatus();
480}
481
482sptr<DisplayLite> DisplayManagerLite::GetDefaultDisplay()
483{
484    return pImpl_->GetDefaultDisplay();
485}
486
487sptr<DisplayLite> DisplayManagerLite::Impl::GetDefaultDisplay()
488{
489    auto displayInfo = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDefaultDisplayInfo();
490    if (displayInfo == nullptr) {
491        return nullptr;
492    }
493    auto displayId = displayInfo->GetDisplayId();
494    std::lock_guard<std::recursive_mutex> lock(mutex_);
495    if (!UpdateDisplayInfoLocked(displayInfo)) {
496        displayMap_.erase(displayId);
497        return nullptr;
498    }
499    return displayMap_[displayId];
500}
501
502bool DisplayManagerLite::IsFoldable()
503{
504    return pImpl_->IsFoldable();
505}
506
507bool DisplayManagerLite::Impl::IsFoldable()
508{
509    return SingletonContainer::Get<DisplayManagerAdapterLite>().IsFoldable();
510}
511
512FoldDisplayMode DisplayManagerLite::GetFoldDisplayMode()
513{
514    return pImpl_->GetFoldDisplayMode();
515}
516
517FoldDisplayMode DisplayManagerLite::Impl::GetFoldDisplayMode()
518{
519    return SingletonContainer::Get<DisplayManagerAdapterLite>().GetFoldDisplayMode();
520}
521
522void DisplayManagerLite::SetFoldDisplayMode(const FoldDisplayMode mode)
523{
524    return pImpl_->SetFoldDisplayMode(mode);
525}
526
527void DisplayManagerLite::Impl::SetFoldDisplayMode(const FoldDisplayMode mode)
528{
529    return SingletonContainer::Get<DisplayManagerAdapterLite>().SetFoldDisplayMode(mode);
530}
531
532void DisplayManagerLite::Impl::OnRemoteDied()
533{
534    WLOGFI("dms is died");
535    std::lock_guard<std::recursive_mutex> lock(mutex_);
536    displayManagerListener_ = nullptr;
537}
538
539void DisplayManagerLite::OnRemoteDied()
540{
541    pImpl_->OnRemoteDied();
542}
543
544sptr<DisplayLite> DisplayManagerLite::Impl::GetDisplayById(DisplayId displayId)
545{
546    WLOGFD("GetDisplayById start, displayId: %{public}" PRIu64" ", displayId);
547    auto displayInfo = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDisplayInfo(displayId);
548    std::lock_guard<std::recursive_mutex> lock(mutex_);
549    if (!UpdateDisplayInfoLocked(displayInfo)) {
550        displayMap_.erase(displayId);
551        return nullptr;
552    }
553    return displayMap_[displayId];
554}
555
556sptr<DisplayLite> DisplayManagerLite::GetDisplayById(DisplayId displayId)
557{
558    if (destroyed_) {
559        return nullptr;
560    }
561    std::lock_guard<std::recursive_mutex> lock(mutex_);
562    return pImpl_->GetDisplayById(displayId);
563}
564
565/*
566 * used by powermgr
567 */
568bool DisplayManagerLite::WakeUpBegin(PowerStateChangeReason reason)
569{
570    WLOGFD("[UL_POWER]WakeUpBegin start, reason:%{public}u", reason);
571    return SingletonContainer::Get<DisplayManagerAdapterLite>().WakeUpBegin(reason);
572}
573
574bool DisplayManagerLite::WakeUpEnd()
575{
576    WLOGFD("[UL_POWER]WakeUpEnd start");
577    return SingletonContainer::Get<DisplayManagerAdapterLite>().WakeUpEnd();
578}
579
580bool DisplayManagerLite::SuspendBegin(PowerStateChangeReason reason)
581{
582    // dms->wms notify other windows to hide
583    WLOGFD("[UL_POWER]SuspendBegin start, reason:%{public}u", reason);
584    return SingletonContainer::Get<DisplayManagerAdapterLite>().SuspendBegin(reason);
585}
586
587bool DisplayManagerLite::SuspendEnd()
588{
589    WLOGFD("[UL_POWER]SuspendEnd start");
590    return SingletonContainer::Get<DisplayManagerAdapterLite>().SuspendEnd();
591}
592
593ScreenId DisplayManagerLite::GetInternalScreenId()
594{
595    WLOGFD("[UL_POWER]GetInternalScreenId start");
596    return SingletonContainer::Get<DisplayManagerAdapterLite>().GetInternalScreenId();
597}
598
599bool DisplayManagerLite::SetScreenPowerById(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason)
600{
601    WLOGFD("[UL_POWER]SetScreenPowerById start");
602    return SingletonContainer::Get<DisplayManagerAdapterLite>().SetScreenPowerById(screenId, state, reason);
603}
604
605bool DisplayManagerLite::SetDisplayState(DisplayState state, DisplayStateCallback callback)
606{
607    return pImpl_->SetDisplayState(state, callback);
608}
609
610DisplayState DisplayManagerLite::GetDisplayState(DisplayId displayId)
611{
612    return SingletonContainer::Get<DisplayManagerAdapterLite>().GetDisplayState(displayId);
613}
614
615bool DisplayManagerLite::Impl::SetDisplayState(DisplayState state, DisplayStateCallback callback)
616{
617    WLOGFD("[UL_POWER]state:%{public}u", state);
618    bool ret = true;
619    {
620        std::lock_guard<std::recursive_mutex> lock(mutex_);
621        if (displayStateCallback_ != nullptr || callback == nullptr) {
622            WLOGFI("[UL_POWER]previous callback not called or callback invalid");
623            if (displayStateCallback_ != nullptr) {
624                WLOGFI("[UL_POWER]previous callback not called, the displayStateCallback_ is not null");
625            }
626            if (callback == nullptr) {
627                WLOGFI("[UL_POWER]Invalid callback received");
628            }
629            return false;
630        }
631        displayStateCallback_ = callback;
632
633        if (displayStateAgent_ == nullptr) {
634            displayStateAgent_ = new DisplayManagerAgent(this);
635            ret = SingletonContainer::Get<DisplayManagerAdapterLite>().RegisterDisplayManagerAgent(
636                displayStateAgent_,
637                DisplayManagerAgentType::DISPLAY_STATE_LISTENER) == DMError::DM_OK;
638        }
639    }
640    ret = ret && SingletonContainer::Get<DisplayManagerAdapterLite>().SetDisplayState(state);
641    if (!ret) {
642        ClearDisplayStateCallback();
643    }
644    return ret;
645}
646
647void DisplayManagerLite::Impl::NotifyDisplayStateChanged(DisplayId id, DisplayState state)
648{
649    WLOGFD("state:%{public}u", state);
650    DisplayStateCallback displayStateCallback = nullptr;
651    {
652        std::lock_guard<std::recursive_mutex> lock(mutex_);
653        displayStateCallback = displayStateCallback_;
654    }
655    if (displayStateCallback) {
656        displayStateCallback(state);
657        ClearDisplayStateCallback();
658        return;
659    }
660    WLOGFW("callback_ target is not set!");
661}
662
663void DisplayManagerLite::Impl::ClearDisplayStateCallback()
664{
665    std::lock_guard<std::recursive_mutex> lock(mutex_);
666    WLOGFD("[UL_POWER]Clear displaystatecallback enter");
667    displayStateCallback_ = nullptr;
668    if (displayStateAgent_ != nullptr) {
669        WLOGFI("[UL_POWER]UnregisterDisplayManagerAgent enter and displayStateAgent_ is cleared");
670        SingletonContainer::Get<DisplayManagerAdapterLite>().UnregisterDisplayManagerAgent(displayStateAgent_,
671            DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
672        displayStateAgent_ = nullptr;
673    }
674}
675
676bool DisplayManagerLite::TryToCancelScreenOff()
677{
678    WLOGFD("[UL_POWER]TryToCancelScreenOff start");
679    return SingletonContainer::Get<DisplayManagerAdapterLite>().TryToCancelScreenOff();
680}
681
682bool DisplayManagerLite::SetScreenBrightness(uint64_t screenId, uint32_t level)
683{
684    WLOGFD("[UL_POWER]SetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
685    SingletonContainer::Get<DisplayManagerAdapterLite>().SetScreenBrightness(screenId, level);
686    return true;
687}
688
689uint32_t DisplayManagerLite::GetScreenBrightness(uint64_t screenId) const
690{
691    uint32_t level = SingletonContainer::Get<DisplayManagerAdapterLite>().GetScreenBrightness(screenId);
692    WLOGFD("[UL_POWER]GetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
693    return level;
694}
695
696DisplayId DisplayManagerLite::GetDefaultDisplayId()
697{
698    auto info = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDefaultDisplayInfo();
699    if (info == nullptr) {
700        return DISPLAY_ID_INVALID;
701    }
702    return info->GetDisplayId();
703}
704
705std::vector<DisplayId> DisplayManagerLite::GetAllDisplayIds()
706{
707    return SingletonContainer::Get<DisplayManagerAdapterLite>().GetAllDisplayIds();
708}
709
710} // namespace OHOS::Rosen