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
16#include "display_manager.h"
17
18#include <chrono>
19#include <cinttypes>
20#include <transaction/rs_interfaces.h>
21#include <ui/rs_surface_node.h>
22
23#include "display_manager_adapter.h"
24#include "display_manager_agent_default.h"
25#include "dm_common.h"
26#include "screen_manager.h"
27#include "singleton_delegator.h"
28#include "window_manager_hilog.h"
29
30namespace OHOS::Rosen {
31namespace {
32constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManager"};
33const static uint32_t MAX_RETRY_NUM = 6;
34const static uint32_t RETRY_WAIT_MS = 500;
35const static uint32_t MAX_DISPLAY_SIZE = 32;
36const static uint32_t MAX_INTERVAL_US = 25000;
37std::atomic<bool> g_dmIsDestroyed = false;
38std::mutex snapBypickerMutex;
39}
40WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManager)
41
42class DisplayManager::Impl : public RefBase {
43public:
44    Impl(std::recursive_mutex& mutex) : mutex_(mutex) {}
45    ~Impl();
46
47    static inline SingletonDelegator<DisplayManager> delegator;
48    bool CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const;
49    bool CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const;
50    sptr<Display> GetDefaultDisplay();
51    sptr<Display> GetDefaultDisplaySync();
52    std::vector<DisplayPhysicalResolution> GetAllDisplayPhysicalResolution();
53    sptr<Display> GetDisplayById(DisplayId displayId);
54    DMError HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow);
55    bool ConvertScreenIdToRsScreenId(ScreenId screenId, ScreenId& rsScreenId);
56    bool IsFoldable();
57    bool IsCaptured();
58    FoldStatus GetFoldStatus();
59    FoldDisplayMode GetFoldDisplayMode();
60    void SetFoldDisplayMode(const FoldDisplayMode);
61    DMError SetFoldDisplayModeFromJs(const FoldDisplayMode);
62    void SetDisplayScale(ScreenId screenId, float scaleX, float scaleY, float pivotX, float pivotY);
63    void SetFoldStatusLocked(bool locked);
64    DMError SetFoldStatusLockedFromJs(bool locked);
65    sptr<FoldCreaseRegion> GetCurrentFoldCreaseRegion();
66    DMError RegisterDisplayListener(sptr<IDisplayListener> listener);
67    DMError UnregisterDisplayListener(sptr<IDisplayListener> listener);
68    bool SetDisplayState(DisplayState state, DisplayStateCallback callback);
69    DMError RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener);
70    DMError UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener);
71    DMError RegisterScreenshotListener(sptr<IScreenshotListener> listener);
72    DMError UnregisterScreenshotListener(sptr<IScreenshotListener> listener);
73    DMError RegisterPrivateWindowListener(sptr<IPrivateWindowListener> listener);
74    DMError UnregisterPrivateWindowListener(sptr<IPrivateWindowListener> listener);
75    DMError RegisterPrivateWindowListChangeListener(sptr<IPrivateWindowListChangeListener> listener);
76    DMError UnregisterPrivateWindowListChangeListener(sptr<IPrivateWindowListChangeListener> listener);
77    DMError RegisterFoldStatusListener(sptr<IFoldStatusListener> listener);
78    DMError UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener);
79    DMError RegisterFoldAngleListener(sptr<IFoldAngleListener> listener);
80    DMError UnregisterFoldAngleListener(sptr<IFoldAngleListener> listener);
81    DMError RegisterCaptureStatusListener(sptr<ICaptureStatusListener> listener);
82    DMError UnregisterCaptureStatusListener(sptr<ICaptureStatusListener> listener);
83    DMError RegisterDisplayUpdateListener(sptr<IDisplayUpdateListener> listener);
84    DMError UnregisterDisplayUpdateListener(sptr<IDisplayUpdateListener> listener);
85    DMError RegisterDisplayModeListener(sptr<IDisplayModeListener> listener);
86    DMError UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener);
87    DMError RegisterAvailableAreaListener(sptr<IAvailableAreaListener> listener);
88    DMError UnregisterAvailableAreaListener(sptr<IAvailableAreaListener> listener);
89    sptr<Display> GetDisplayByScreenId(ScreenId screenId);
90    DMError ProxyForFreeze(const std::set<int32_t>& pidList, bool isProxy);
91    DMError ResetAllFreezeStatus();
92    DMError SetVirtualScreenSecurityExemption(ScreenId screenId, uint32_t pid, std::vector<uint64_t>& windowIdList);
93    void OnRemoteDied();
94private:
95    void ClearDisplayStateCallback();
96    void ClearFoldStatusCallback();
97    void ClearFoldAngleCallback();
98    void ClearCaptureStatusCallback();
99    void ClearDisplayModeCallback();
100    void NotifyPrivateWindowStateChanged(bool hasPrivate);
101    void NotifyPrivateStateWindowListChanged(DisplayId id, std::vector<std::string> privacyWindowList);
102    void NotifyScreenshot(sptr<ScreenshotInfo> info);
103    void NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status);
104    void NotifyDisplayStateChanged(DisplayId id, DisplayState state);
105    void NotifyDisplayChangedEvent(sptr<DisplayInfo> info, DisplayChangeEvent event);
106    void NotifyDisplayCreate(sptr<DisplayInfo> info);
107    void NotifyDisplayDestroy(DisplayId);
108    void NotifyDisplayChange(sptr<DisplayInfo> displayInfo);
109    bool UpdateDisplayInfoLocked(sptr<DisplayInfo>);
110    void NotifyFoldStatusChanged(FoldStatus foldStatus);
111    void NotifyFoldAngleChanged(std::vector<float> foldAngles);
112    void NotifyCaptureStatusChanged(bool isCapture);
113    void NotifyDisplayChangeInfoChanged(const sptr<DisplayChangeInfo>& info);
114    void NotifyDisplayModeChanged(FoldDisplayMode displayMode);
115    void NotifyAvailableAreaChanged(DMRect rect);
116    void Clear();
117    std::string GetDisplayInfoSrting(sptr<DisplayInfo> displayInfo);
118
119    DisplayId defaultDisplayId_ = DISPLAY_ID_INVALID;
120    std::map<DisplayId, sptr<Display>> displayMap_;
121    DisplayStateCallback displayStateCallback_;
122    std::recursive_mutex& mutex_;
123    std::set<sptr<IDisplayListener>> displayListeners_;
124    std::set<sptr<IDisplayPowerEventListener>> powerEventListeners_;
125    std::set<sptr<IScreenshotListener>> screenshotListeners_;
126    std::set<sptr<IPrivateWindowListener>> privateWindowListeners_;
127    std::set<sptr<IPrivateWindowListChangeListener>> privateWindowListChangeListeners_;
128    std::set<sptr<IFoldStatusListener>> foldStatusListeners_;
129    std::set<sptr<IFoldAngleListener>> foldAngleListeners_;
130    std::set<sptr<ICaptureStatusListener>> captureStatusListeners_;
131    std::set<sptr<IDisplayUpdateListener>> displayUpdateListeners_;
132    std::set<sptr<IDisplayModeListener>> displayModeListeners_;
133    std::set<sptr<IAvailableAreaListener>> availableAreaListeners_;
134    class DisplayManagerListener;
135    sptr<DisplayManagerListener> displayManagerListener_;
136    class DisplayManagerAgent;
137    sptr<DisplayManagerAgent> displayStateAgent_;
138    sptr<DisplayManagerAgent> powerEventListenerAgent_;
139    class DisplayManagerScreenshotAgent;
140    sptr<DisplayManagerScreenshotAgent> screenshotListenerAgent_;
141    class DisplayManagerPrivateWindowAgent;
142    sptr<DisplayManagerPrivateWindowAgent> privateWindowListenerAgent_;
143    class DisplayManagerPrivateWindowListAgent;
144    sptr<DisplayManagerPrivateWindowListAgent> privateWindowListChangeListenerAgent_;
145    class DisplayManagerFoldStatusAgent;
146    sptr<DisplayManagerFoldStatusAgent> foldStatusListenerAgent_;
147    class DisplayManagerFoldAngleAgent;
148    sptr<DisplayManagerFoldAngleAgent> foldAngleListenerAgent_;
149    class DisplayManagerCaptureStatusAgent;
150    sptr<DisplayManagerCaptureStatusAgent> captureStatusListenerAgent_;
151    class DisplayManagerDisplayUpdateAgent;
152    sptr<DisplayManagerDisplayUpdateAgent> displayUpdateListenerAgent_;
153    class DisplayManagerDisplayModeAgent;
154    sptr<DisplayManagerDisplayModeAgent> displayModeListenerAgent_;
155    class DisplayManagerAvailableAreaAgent;
156    sptr<DisplayManagerAvailableAreaAgent> availableAreaListenerAgent_;
157};
158
159class DisplayManager::Impl::DisplayManagerListener : public DisplayManagerAgentDefault {
160public:
161    explicit DisplayManagerListener(sptr<Impl> impl) : pImpl_(impl)
162    {
163    }
164
165    void OnDisplayCreate(sptr<DisplayInfo> displayInfo) override
166    {
167        if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
168            WLOGFE("OnDisplayCreate, displayInfo is invalid.");
169            return;
170        }
171        if (pImpl_ == nullptr) {
172            WLOGFE("OnDisplayCreate, impl is nullptr.");
173            return;
174        }
175        pImpl_->NotifyDisplayCreate(displayInfo);
176        std::set<sptr<IDisplayListener>> displayListeners;
177        {
178            std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
179            displayListeners = pImpl_->displayListeners_;
180        }
181        for (auto listener : displayListeners) {
182            listener->OnCreate(displayInfo->GetDisplayId());
183        }
184    };
185
186    void OnDisplayDestroy(DisplayId displayId) override
187    {
188        if (displayId == DISPLAY_ID_INVALID) {
189            WLOGFE("OnDisplayDestroy, displayId is invalid.");
190            return;
191        }
192        if (pImpl_ == nullptr) {
193            WLOGFE("OnDisplayDestroy, impl is nullptr.");
194            return;
195        }
196        pImpl_->NotifyDisplayDestroy(displayId);
197        std::set<sptr<IDisplayListener>> displayListeners;
198        {
199            std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
200            displayListeners = pImpl_->displayListeners_;
201        }
202        for (auto listener : displayListeners) {
203            listener->OnDestroy(displayId);
204        }
205    };
206
207    void OnDisplayChange(sptr<DisplayInfo> displayInfo, DisplayChangeEvent event) override
208    {
209        if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
210            WLOGFE("OnDisplayChange, displayInfo is invalid.");
211            return;
212        }
213        if (pImpl_ == nullptr) {
214            WLOGFE("OnDisplayChange, impl is nullptr.");
215            return;
216        }
217        WLOGD("OnDisplayChange. display %{public}" PRIu64", event %{public}u", displayInfo->GetDisplayId(), event);
218        pImpl_->NotifyDisplayChange(displayInfo);
219        std::set<sptr<IDisplayListener>> displayListeners;
220        {
221            std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
222            displayListeners = pImpl_->displayListeners_;
223        }
224        for (auto listener : displayListeners) {
225            listener->OnChange(displayInfo->GetDisplayId());
226        }
227    };
228private:
229    sptr<Impl> pImpl_;
230};
231
232class DisplayManager::Impl::DisplayManagerAgent : public DisplayManagerAgentDefault {
233public:
234    explicit DisplayManagerAgent(sptr<Impl> impl) : pImpl_(impl)
235    {
236    }
237    ~DisplayManagerAgent() = default;
238
239    virtual void NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) override
240    {
241        pImpl_->NotifyDisplayPowerEvent(event, status);
242    }
243
244    virtual void NotifyDisplayStateChanged(DisplayId id, DisplayState state) override
245    {
246        pImpl_->NotifyDisplayStateChanged(id, state);
247    }
248private:
249    sptr<Impl> pImpl_;
250};
251
252class DisplayManager::Impl::DisplayManagerScreenshotAgent : public DisplayManagerAgentDefault {
253public:
254    explicit DisplayManagerScreenshotAgent(sptr<Impl> impl) : pImpl_(impl)
255    {
256    }
257    ~DisplayManagerScreenshotAgent() = default;
258
259    virtual void OnScreenshot(sptr<ScreenshotInfo> info) override
260    {
261        pImpl_->NotifyScreenshot(info);
262    }
263private:
264    sptr<Impl> pImpl_;
265};
266
267class DisplayManager::Impl::DisplayManagerPrivateWindowAgent : public DisplayManagerAgentDefault {
268public:
269    explicit DisplayManagerPrivateWindowAgent(sptr<Impl> impl) : pImpl_(impl)
270    {
271    }
272    ~DisplayManagerPrivateWindowAgent() = default;
273
274    virtual void NotifyPrivateWindowStateChanged(bool hasPrivate) override
275    {
276        pImpl_->NotifyPrivateWindowStateChanged(hasPrivate);
277    }
278private:
279    sptr<Impl> pImpl_;
280};
281
282class DisplayManager::Impl::DisplayManagerPrivateWindowListAgent : public DisplayManagerAgentDefault {
283public:
284    explicit DisplayManagerPrivateWindowListAgent(sptr<Impl> impl) : pImpl_(impl)
285    {
286    }
287    ~DisplayManagerPrivateWindowListAgent() = default;
288
289    virtual void NotifyPrivateStateWindowListChanged(DisplayId id, std::vector<std::string> privacyWindowList) override
290    {
291        pImpl_->NotifyPrivateStateWindowListChanged(id, privacyWindowList);
292    }
293private:
294    sptr<Impl> pImpl_;
295};
296
297class DisplayManager::Impl::DisplayManagerFoldStatusAgent : public DisplayManagerAgentDefault {
298public:
299    explicit DisplayManagerFoldStatusAgent(sptr<Impl> impl) : pImpl_(impl)
300    {
301    }
302    ~DisplayManagerFoldStatusAgent() = default;
303
304    virtual void NotifyFoldStatusChanged(FoldStatus foldStatus) override
305    {
306        pImpl_->NotifyFoldStatusChanged(foldStatus);
307    }
308private:
309    sptr<Impl> pImpl_;
310};
311
312class DisplayManager::Impl::DisplayManagerFoldAngleAgent : public DisplayManagerAgentDefault {
313public:
314    explicit DisplayManagerFoldAngleAgent(sptr<Impl> impl) : pImpl_(impl)
315    {
316    }
317    ~DisplayManagerFoldAngleAgent() = default;
318
319    virtual void NotifyFoldAngleChanged(std::vector<float> foldAngles) override
320    {
321        pImpl_->NotifyFoldAngleChanged(foldAngles);
322    }
323private:
324    sptr<Impl> pImpl_;
325};
326
327class DisplayManager::Impl::DisplayManagerCaptureStatusAgent : public DisplayManagerAgentDefault {
328public:
329    explicit DisplayManagerCaptureStatusAgent(sptr<Impl> impl) : pImpl_(impl)
330    {
331    }
332    ~DisplayManagerCaptureStatusAgent() = default;
333
334    virtual void NotifyCaptureStatusChanged(bool isCapture) override
335    {
336        pImpl_->NotifyCaptureStatusChanged(isCapture);
337    }
338private:
339    sptr<Impl> pImpl_;
340};
341
342class DisplayManager::Impl::DisplayManagerDisplayUpdateAgent : public DisplayManagerAgentDefault {
343public:
344    explicit DisplayManagerDisplayUpdateAgent(sptr<Impl> impl) : pImpl_(impl)
345    {
346    }
347    ~DisplayManagerDisplayUpdateAgent() = default;
348
349    virtual void NotifyDisplayChangeInfoChanged(const sptr<DisplayChangeInfo>& info) override
350    {
351        pImpl_->NotifyDisplayChangeInfoChanged(info);
352    }
353private:
354    sptr<Impl> pImpl_;
355};
356
357
358class DisplayManager::Impl::DisplayManagerDisplayModeAgent : public DisplayManagerAgentDefault {
359public:
360    explicit DisplayManagerDisplayModeAgent(sptr<Impl> impl) : pImpl_(impl)
361    {
362    }
363    ~DisplayManagerDisplayModeAgent() = default;
364
365    virtual void NotifyDisplayModeChanged(FoldDisplayMode displayMode) override
366    {
367        pImpl_->NotifyDisplayModeChanged(displayMode);
368    }
369private:
370    sptr<Impl> pImpl_;
371};
372
373class DisplayManager::Impl::DisplayManagerAvailableAreaAgent : public DisplayManagerAgentDefault {
374public:
375    explicit DisplayManagerAvailableAreaAgent(sptr<Impl> impl) : pImpl_(impl)
376    {
377    }
378    ~DisplayManagerAvailableAreaAgent() = default;
379
380    virtual void NotifyAvailableAreaChanged(DMRect area) override
381    {
382        pImpl_->NotifyAvailableAreaChanged(area);
383    }
384private:
385    sptr<Impl> pImpl_;
386};
387
388bool DisplayManager::Impl::CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const
389{
390    if (rect.left < 0) {
391        return false;
392    }
393    if (rect.top < 0) {
394        return false;
395    }
396    if (rect.width < 0) {
397        return false;
398    }
399    if (rect.height < 0) {
400        return false;
401    }
402    if (rect.width + rect.left > oriWidth) {
403        return false;
404    }
405    if (rect.height + rect.top > oriHeight) {
406        return false;
407    }
408    return true;
409}
410
411bool DisplayManager::Impl::CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const
412{
413    if (size.width < 0) {
414        return false;
415    }
416    if (size.height < 0) {
417        return false;
418    }
419    if (size.width > MAX_RESOLUTION_SIZE_SCREENSHOT) {
420        return false;
421    }
422    if (size.height > MAX_RESOLUTION_SIZE_SCREENSHOT) {
423        return false;
424    }
425    return true;
426}
427
428void DisplayManager::Impl::ClearDisplayStateCallback()
429{
430    std::lock_guard<std::recursive_mutex> lock(mutex_);
431    WLOGFD("[UL_POWER]Clear displaystatecallback enter");
432    displayStateCallback_ = nullptr;
433    if (displayStateAgent_ != nullptr) {
434        WLOGFI("[UL_POWER]UnregisterDisplayManagerAgent enter and displayStateAgent_ is cleared");
435        SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(displayStateAgent_,
436            DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
437        displayStateAgent_ = nullptr;
438    }
439}
440
441void DisplayManager::Impl::ClearFoldStatusCallback()
442{
443    DMError res = DMError::DM_OK;
444    std::lock_guard<std::recursive_mutex> lock(mutex_);
445    if (foldStatusListenerAgent_ != nullptr) {
446        res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(foldStatusListenerAgent_,
447            DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
448        foldStatusListenerAgent_ = nullptr;
449        WLOGI("ClearFoldStatusCallback foldStatusListenerAgent_ is nullptr !");
450    }
451    if (res != DMError::DM_OK) {
452        WLOGFW("UnregisterDisplayManagerAgent FOLD_STATUS_CHANGED_LISTENER failed !");
453    }
454}
455
456void DisplayManager::Impl::ClearFoldAngleCallback()
457{
458    std::lock_guard<std::recursive_mutex> lock(mutex_);
459    if (foldAngleListenerAgent_ == nullptr) {
460        return;
461    }
462    DMError res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
463        foldAngleListenerAgent_, DisplayManagerAgentType::FOLD_ANGLE_CHANGED_LISTENER);
464    foldAngleListenerAgent_ = nullptr;
465    if (res != DMError::DM_OK) {
466        WLOGFE("ClearFoldAngleCallback FOLD_ANGLE_CHANGED_LISTENER failed !");
467    } else {
468        WLOGI("ClearFoldAngleCallback foldAngleListenerAgent_!");
469    }
470}
471
472void DisplayManager::Impl::ClearCaptureStatusCallback()
473{
474    std::lock_guard<std::recursive_mutex> lock(mutex_);
475    if (captureStatusListenerAgent_ == nullptr) {
476        return;
477    }
478    DMError res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
479        captureStatusListenerAgent_, DisplayManagerAgentType::CAPTURE_STATUS_CHANGED_LISTENER);
480    captureStatusListenerAgent_ = nullptr;
481    if (res != DMError::DM_OK) {
482        WLOGFE("ClearCaptureStatusCallback FOLD_ANGLE_CHANGED_LISTENER failed !");
483    } else {
484        WLOGI("ClearCaptureStatusCallback captureStatusListenerAgent_!");
485    }
486}
487
488void DisplayManager::Impl::ClearDisplayModeCallback()
489{
490    DMError res = DMError::DM_OK;
491    std::lock_guard<std::recursive_mutex> lock(mutex_);
492    if (displayModeListenerAgent_ != nullptr) {
493        res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(displayModeListenerAgent_,
494            DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
495        displayModeListenerAgent_ = nullptr;
496        WLOGI("ClearDisplayStateCallback displayModeListenerAgent_ is nullptr !");
497    }
498    if (res != DMError::DM_OK) {
499        WLOGFW("UnregisterDisplayManagerAgent DISPLAY_MODE_CHANGED_LISTENER failed !");
500    }
501}
502
503void DisplayManager::Impl::Clear()
504{
505    WLOGFI("Clear displaymanager listener");
506    std::lock_guard<std::recursive_mutex> lock(mutex_);
507    DMError res = DMError::DM_OK;
508    if (displayManagerListener_ != nullptr) {
509        res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
510            displayManagerListener_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
511    }
512    displayManagerListener_ = nullptr;
513    if (res != DMError::DM_OK) {
514        WLOGFW("UnregisterDisplayManagerAgent DISPLAY_EVENT_LISTENER failed !");
515    }
516    res = DMError::DM_OK;
517    if (powerEventListenerAgent_ != nullptr) {
518        res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
519            powerEventListenerAgent_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
520    }
521    powerEventListenerAgent_ = nullptr;
522    if (res != DMError::DM_OK) {
523        WLOGFW("UnregisterDisplayManagerAgent DISPLAY_POWER_EVENT_LISTENER failed !");
524    }
525    ClearDisplayStateCallback();
526    ClearFoldStatusCallback();
527    ClearFoldAngleCallback();
528    ClearCaptureStatusCallback();
529    ClearDisplayModeCallback();
530}
531
532DisplayManager::Impl::~Impl()
533{
534    Clear();
535}
536
537DisplayManager::DisplayManager() : pImpl_(new Impl(mutex_))
538{
539    WLOGFD("Create displaymanager instance");
540    g_dmIsDestroyed = false;
541}
542
543DisplayManager::~DisplayManager()
544{
545    WLOGFI("Destroy displaymanager instance");
546    g_dmIsDestroyed = true;
547}
548
549DisplayId DisplayManager::GetDefaultDisplayId()
550{
551    auto info = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
552    if (info == nullptr) {
553        return DISPLAY_ID_INVALID;
554    }
555    return info->GetDisplayId();
556}
557
558sptr<Display> DisplayManager::Impl::GetDefaultDisplay()
559{
560    auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
561    if (displayInfo == nullptr) {
562        return nullptr;
563    }
564    auto displayId = displayInfo->GetDisplayId();
565    std::lock_guard<std::recursive_mutex> lock(mutex_);
566    if (!UpdateDisplayInfoLocked(displayInfo)) {
567        displayMap_.erase(displayId);
568        return nullptr;
569    }
570    return displayMap_[displayId];
571}
572
573sptr<Display> DisplayManager::Impl::GetDefaultDisplaySync()
574{
575    static std::chrono::steady_clock::time_point lastRequestTime = std::chrono::steady_clock::now();
576    auto currentTime = std::chrono::steady_clock::now();
577    auto interval = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - lastRequestTime).count();
578    if (defaultDisplayId_ != DISPLAY_ID_INVALID && interval < MAX_INTERVAL_US) {
579        std::lock_guard<std::recursive_mutex> lock(mutex_);
580        auto iter = displayMap_.find(defaultDisplayId_);
581        if (iter != displayMap_.end()) {
582            return displayMap_[defaultDisplayId_];
583        }
584    }
585
586    uint32_t retryTimes = 0;
587    sptr<DisplayInfo> displayInfo = nullptr;
588    while (retryTimes < MAX_RETRY_NUM) {
589        displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
590        if (displayInfo != nullptr) {
591            break;
592        }
593        retryTimes++;
594        WLOGFW("Current get display info is null, retry %{public}u times", retryTimes);
595        std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_WAIT_MS));
596    }
597    if (retryTimes >= MAX_RETRY_NUM || displayInfo == nullptr) {
598        WLOGFE("Get display info failed, please check whether the onscreenchange event is triggered");
599        return nullptr;
600    }
601
602    auto displayId = displayInfo->GetDisplayId();
603    std::lock_guard<std::recursive_mutex> lock(mutex_);
604    if (!UpdateDisplayInfoLocked(displayInfo)) {
605        displayMap_.erase(displayId);
606        return nullptr;
607    }
608    lastRequestTime = currentTime;
609    defaultDisplayId_ = displayId;
610    return displayMap_[displayId];
611}
612
613sptr<Display> DisplayManager::Impl::GetDisplayById(DisplayId displayId)
614{
615    WLOGFD("GetDisplayById start, displayId: %{public}" PRIu64" ", displayId);
616    auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfo(displayId);
617    std::lock_guard<std::recursive_mutex> lock(mutex_);
618    if (!UpdateDisplayInfoLocked(displayInfo)) {
619        displayMap_.erase(displayId);
620        return nullptr;
621    }
622    return displayMap_[displayId];
623}
624
625sptr<Display> DisplayManager::GetDisplayById(DisplayId displayId)
626{
627    if (g_dmIsDestroyed) {
628        WLOGFI("DM has been destructed");
629        return nullptr;
630    }
631    std::lock_guard<std::recursive_mutex> lock(mutex_);
632    return pImpl_->GetDisplayById(displayId);
633}
634
635sptr<Display> DisplayManager::GetDisplayByScreen(ScreenId screenId)
636{
637    if (screenId == SCREEN_ID_INVALID) {
638        WLOGFE("screenId is invalid.");
639        return nullptr;
640    }
641    sptr<Display> display = pImpl_->GetDisplayByScreenId(screenId);
642    if (display == nullptr) {
643        WLOGFE("get display by screenId failed. screen %{public}" PRIu64"", screenId);
644    }
645    return display;
646}
647
648sptr<Display> DisplayManager::Impl::GetDisplayByScreenId(ScreenId screenId)
649{
650    sptr<DisplayInfo> displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfoByScreenId(screenId);
651    if (displayInfo == nullptr) {
652        WLOGFE("get display by screenId: displayInfo is null");
653        return nullptr;
654    }
655    DisplayId displayId = displayInfo->GetDisplayId();
656    if (displayId == DISPLAY_ID_INVALID) {
657        WLOGFE("get display by screenId: invalid displayInfo");
658        return nullptr;
659    }
660
661    std::lock_guard<std::recursive_mutex> lock(mutex_);
662    if (!UpdateDisplayInfoLocked(displayInfo)) {
663        displayMap_.erase(displayId);
664        return nullptr;
665    }
666    return displayMap_[displayId];
667}
668
669std::shared_ptr<Media::PixelMap> DisplayManager::GetScreenshot(DisplayId displayId, DmErrorCode* errorCode)
670{
671    if (displayId == DISPLAY_ID_INVALID) {
672        WLOGFE("displayId invalid!");
673        return nullptr;
674    }
675    std::shared_ptr<Media::PixelMap> screenShot =
676        SingletonContainer::Get<DisplayManagerAdapter>().GetDisplaySnapshot(displayId, errorCode);
677    if (screenShot == nullptr) {
678        WLOGFE("DisplayManager::GetScreenshot failed!");
679        return nullptr;
680    }
681
682    return screenShot;
683}
684
685std::shared_ptr<Media::PixelMap> DisplayManager::GetSnapshotByPicker(Media::Rect &rect, DmErrorCode* errorCode)
686{
687    std::unique_lock<std::mutex> lock(snapBypickerMutex, std::defer_lock);
688    if (!lock.try_lock()) {
689        WLOGFE("DisplayManager::GetSnapshotByPicker try_lock failed!");
690        return nullptr;
691    }
692    std::shared_ptr<Media::PixelMap> screenShot =
693        SingletonContainer::Get<DisplayManagerAdapter>().GetSnapshotByPicker(rect, errorCode);
694    lock.unlock();
695    if (screenShot == nullptr) {
696        WLOGFE("DisplayManager::GetSnapshotByPicker failed!");
697        return nullptr;
698    }
699    WLOGFI("snapshot area left:%{public}d, top:%{public}d, width:%{public}d, height:%{public}d",
700        rect.left, rect.top, rect.width, rect.height);
701    // create crop pixel map
702    if (rect.width == 0 || rect.height == 0) {
703        WLOGFE("width or height is invalid!");
704        return nullptr;
705    }
706    Media::InitializationOptions opt;
707    opt.size.width = rect.width;
708    opt.size.height = rect.height;
709    opt.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
710    opt.editable = false;
711    auto pixelMap = Media::PixelMap::Create(*screenShot, rect, opt);
712    if (pixelMap == nullptr) {
713        WLOGFE("Media pixel map create failed");
714        return nullptr;
715    }
716    return pixelMap;
717}
718
719std::shared_ptr<Media::PixelMap> DisplayManager::GetScreenshot(DisplayId displayId, const Media::Rect &rect,
720    const Media::Size &size, int rotation, DmErrorCode* errorCode)
721{
722    std::shared_ptr<Media::PixelMap> screenShot = GetScreenshot(displayId, errorCode);
723    if (screenShot == nullptr) {
724        WLOGFE("DisplayManager::GetScreenshot failed!");
725        return nullptr;
726    }
727
728    // check parameters
729    int32_t oriHeight = screenShot->GetHeight();
730    int32_t oriWidth = screenShot->GetWidth();
731    if (!pImpl_->CheckRectValid(rect, oriHeight, oriWidth)) {
732        WLOGFE("rect invalid! left %{public}d, top %{public}d, w %{public}d, h %{public}d",
733            rect.left, rect.top, rect.width, rect.height);
734        return nullptr;
735    }
736    if (!pImpl_->CheckSizeValid(size, oriHeight, oriWidth)) {
737        WLOGFE("size invalid! w %{public}d, h %{public}d", rect.width, rect.height);
738        return nullptr;
739    }
740
741    // create crop dest pixelmap
742    Media::InitializationOptions opt;
743    opt.size.width = size.width;
744    opt.size.height = size.height;
745    opt.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
746    opt.editable = false;
747    auto pixelMap = Media::PixelMap::Create(*screenShot, rect, opt);
748    if (pixelMap == nullptr) {
749        WLOGFE("Media::PixelMap::Create failed!");
750        return nullptr;
751    }
752    std::shared_ptr<Media::PixelMap> dstScreenshot(pixelMap.release());
753
754    return dstScreenshot;
755}
756
757sptr<Display> DisplayManager::GetDefaultDisplay()
758{
759    return pImpl_->GetDefaultDisplay();
760}
761
762void DisplayManager::AddDisplayIdFromAms(DisplayId displayId, const wptr<IRemoteObject>& abilityToken)
763{
764    if (abilityToken == nullptr || displayId == DISPLAY_ID_INVALID) {
765        WLOGFE("abilityToken is nullptr or display id invalid.");
766        return;
767    }
768    std::lock_guard<std::mutex> lock(displayOperateMutex_);
769    auto iter = std::find_if(displayIdList_.begin(), displayIdList_.end(),
770        [displayId, abilityToken](const auto &item) -> bool {
771            return item.first == abilityToken && item.second == displayId;
772    });
773    if (iter != displayIdList_.end()) {
774        WLOGFI("abilityToken and display[%{public}" PRIu64"] has been added.", displayId);
775    } else {
776        displayIdList_.push_back(std::make_pair(abilityToken, displayId));
777    }
778    ShowDisplayIdList(true);
779}
780
781void DisplayManager::RemoveDisplayIdFromAms(const wptr<IRemoteObject>& abilityToken)
782{
783    if (abilityToken == nullptr) {
784        WLOGFE("abilityToken is nullptr.");
785        return;
786    }
787    std::lock_guard<std::mutex> lock(displayOperateMutex_);
788    if (displayIdList_.empty()) {
789        WLOGFI("displayIdList_ is empty.");
790        return;
791    }
792    auto iter = std::find_if(displayIdList_.begin(), displayIdList_.end(),
793        [abilityToken](const auto &item) -> bool {
794            return item.first == abilityToken;
795    });
796    if (iter != displayIdList_.end()) {
797        displayIdList_.erase(iter);
798    }
799    ShowDisplayIdList(true);
800}
801
802DisplayId DisplayManager::GetCallingAbilityDisplayId()
803{
804    DisplayId displayId = DISPLAY_ID_INVALID;
805    std::lock_guard<std::mutex> lock(displayOperateMutex_);
806    if (displayIdList_.empty()) {
807        WLOGFI("displayIdList is empty.");
808        return displayId;
809    }
810    int displayCount = 0;
811    for (const auto &iter : displayIdList_) {
812        if (displayId == DISPLAY_ID_INVALID || displayId != iter.second) {
813            displayCount++;
814        }
815        displayId = iter.second;
816        if (displayCount > 1) {
817            break;
818        }
819    }
820    ShowDisplayIdList(false);
821    return displayCount == 1 ? displayId : DISPLAY_ID_INVALID;
822}
823
824void DisplayManager::ShowDisplayIdList(bool isShowLog)
825{
826    std::ostringstream oss;
827    oss << "current display id list:[";
828    for (const auto &iter : displayIdList_) {
829        oss << iter.second << ",";
830    }
831    if (isShowLog) {
832        WLOGFD("%{public}s]", oss.str().c_str());
833    } else {
834        WLOGFI("%{public}s]", oss.str().c_str());
835    }
836}
837
838sptr<Display> DisplayManager::GetDefaultDisplaySync(bool isFromNapi)
839{
840    if (isFromNapi) {
841        sptr<Display> display = nullptr;
842        DisplayId displayId = GetCallingAbilityDisplayId();
843        if (displayId != DISPLAY_ID_INVALID) {
844            WLOGFI("displayId:%{public}" PRIu64, displayId);
845            display = pImpl_->GetDisplayById(displayId);
846        }
847        if (display != nullptr) {
848            return display;
849        } else {
850            WLOGFI("get displayId:%{public}" PRIu64" info nullptr.", displayId);
851        }
852    }
853    return pImpl_->GetDefaultDisplaySync();
854}
855
856std::vector<DisplayId> DisplayManager::GetAllDisplayIds()
857{
858    return SingletonContainer::Get<DisplayManagerAdapter>().GetAllDisplayIds();
859}
860
861std::vector<DisplayPhysicalResolution> DisplayManager::Impl::GetAllDisplayPhysicalResolution()
862{
863    return SingletonContainer::Get<DisplayManagerAdapter>().GetAllDisplayPhysicalResolution();
864}
865
866std::vector<DisplayPhysicalResolution> DisplayManager::GetAllDisplayPhysicalResolution()
867{
868    return pImpl_->GetAllDisplayPhysicalResolution();
869}
870
871std::vector<sptr<Display>> DisplayManager::GetAllDisplays()
872{
873    WLOGFD("GetAllDisplays start");
874    std::vector<sptr<Display>> res;
875    auto displayIds = GetAllDisplayIds();
876    for (auto displayId : displayIds) {
877        const sptr<Display> display = GetDisplayById(displayId);
878        if (display != nullptr) {
879            res.emplace_back(display);
880        } else {
881            WLOGFE("DisplayManager::GetAllDisplays display %" PRIu64" nullptr!", displayId);
882        }
883    }
884    return res;
885}
886
887DMError DisplayManager::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
888{
889    return pImpl_->HasPrivateWindow(displayId, hasPrivateWindow);
890}
891
892DMError DisplayManager::Impl::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
893{
894    return SingletonContainer::Get<DisplayManagerAdapter>().HasPrivateWindow(displayId, hasPrivateWindow);
895}
896
897bool DisplayManager::IsFoldable()
898{
899    return pImpl_->IsFoldable();
900}
901
902bool DisplayManager::Impl::IsFoldable()
903{
904    return SingletonContainer::Get<DisplayManagerAdapter>().IsFoldable();
905}
906
907bool DisplayManager::IsCaptured()
908{
909    return pImpl_->IsCaptured();
910}
911
912bool DisplayManager::Impl::IsCaptured()
913{
914    return SingletonContainer::Get<DisplayManagerAdapter>().IsCaptured();
915}
916
917FoldStatus DisplayManager::GetFoldStatus()
918{
919    return pImpl_->GetFoldStatus();
920}
921
922FoldStatus DisplayManager::Impl::GetFoldStatus()
923{
924    return SingletonContainer::Get<DisplayManagerAdapter>().GetFoldStatus();
925}
926
927FoldDisplayMode DisplayManager::GetFoldDisplayMode()
928{
929    return pImpl_->GetFoldDisplayMode();
930}
931
932FoldDisplayMode DisplayManager::Impl::GetFoldDisplayMode()
933{
934    return SingletonContainer::Get<DisplayManagerAdapter>().GetFoldDisplayMode();
935}
936
937void DisplayManager::SetFoldDisplayMode(const FoldDisplayMode mode)
938{
939    pImpl_->SetFoldDisplayMode(mode);
940}
941
942DMError DisplayManager::SetFoldDisplayModeFromJs(const FoldDisplayMode mode)
943{
944    return pImpl_->SetFoldDisplayModeFromJs(mode);
945}
946
947void DisplayManager::SetDisplayScale(ScreenId screenId, float scaleX, float scaleY, float pivotX, float pivotY)
948{
949    pImpl_->SetDisplayScale(screenId, scaleX, scaleY, pivotX, pivotY);
950}
951
952void DisplayManager::Impl::SetDisplayScale(ScreenId screenId,
953    float scaleX, float scaleY, float pivotX, float pivotY)
954{
955    SingletonContainer::Get<DisplayManagerAdapter>().SetDisplayScale(screenId, scaleX, scaleY, pivotX, pivotY);
956}
957
958void DisplayManager::Impl::SetFoldDisplayMode(const FoldDisplayMode mode)
959{
960    SingletonContainer::Get<DisplayManagerAdapter>().SetFoldDisplayMode(mode);
961}
962
963DMError DisplayManager::Impl::SetFoldDisplayModeFromJs(const FoldDisplayMode mode)
964{
965    return SingletonContainer::Get<DisplayManagerAdapter>().SetFoldDisplayModeFromJs(mode);
966}
967
968void DisplayManager::SetFoldStatusLocked(bool locked)
969{
970    pImpl_->SetFoldStatusLocked(locked);
971}
972
973DMError DisplayManager::SetFoldStatusLockedFromJs(bool locked)
974{
975    return pImpl_->SetFoldStatusLockedFromJs(locked);
976}
977
978void DisplayManager::Impl::SetFoldStatusLocked(bool locked)
979{
980    SingletonContainer::Get<DisplayManagerAdapter>().SetFoldStatusLocked(locked);
981}
982
983DMError DisplayManager::Impl::SetFoldStatusLockedFromJs(bool locked)
984{
985    return SingletonContainer::Get<DisplayManagerAdapter>().SetFoldStatusLockedFromJs(locked);
986}
987
988sptr<FoldCreaseRegion> DisplayManager::GetCurrentFoldCreaseRegion()
989{
990    return pImpl_->GetCurrentFoldCreaseRegion();
991}
992
993sptr<FoldCreaseRegion> DisplayManager::Impl::GetCurrentFoldCreaseRegion()
994{
995    return SingletonContainer::Get<DisplayManagerAdapter>().GetCurrentFoldCreaseRegion();
996}
997
998DMError DisplayManager::Impl::RegisterDisplayListener(sptr<IDisplayListener> listener)
999{
1000    std::lock_guard<std::recursive_mutex> lock(mutex_);
1001    DMError ret = DMError::DM_OK;
1002    if (displayManagerListener_ == nullptr) {
1003        displayManagerListener_ = new DisplayManagerListener(this);
1004        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1005            displayManagerListener_,
1006            DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
1007    }
1008    if (ret != DMError::DM_OK) {
1009        WLOGFW("RegisterDisplayManagerAgent failed !");
1010        displayManagerListener_ = nullptr;
1011    } else {
1012        displayListeners_.insert(listener);
1013    }
1014    return ret;
1015}
1016
1017void DisplayManager::Impl::NotifyPrivateWindowStateChanged(bool hasPrivate)
1018{
1019    std::set<sptr<IPrivateWindowListener>> privateWindowListeners;
1020    {
1021        std::lock_guard<std::recursive_mutex> lock(mutex_);
1022        privateWindowListeners = privateWindowListeners_;
1023    }
1024    for (auto& listener : privateWindowListeners) {
1025        listener->OnPrivateWindow(hasPrivate);
1026    }
1027}
1028
1029void DisplayManager::Impl::NotifyPrivateStateWindowListChanged(DisplayId id, std::vector<std::string> privacyWindowList)
1030{
1031    std::set<sptr<IPrivateWindowListChangeListener>> privateWindowListChangeListeners;
1032    {
1033        std::lock_guard<std::recursive_mutex> lock(mutex_);
1034        privateWindowListChangeListeners = privateWindowListChangeListeners_;
1035    }
1036    for (auto& listener : privateWindowListChangeListeners) {
1037        listener->OnPrivateWindowListChange(id, privacyWindowList);
1038    }
1039}
1040
1041DMError DisplayManager::RegisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)
1042{
1043    if (listener == nullptr) {
1044        WLOGFE("RegisterPrivateWindowListener listener is nullptr.");
1045        return DMError::DM_ERROR_NULLPTR;
1046    }
1047    return pImpl_->RegisterPrivateWindowListener(listener);
1048}
1049
1050DMError DisplayManager::UnregisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)
1051{
1052    if (listener == nullptr) {
1053        WLOGFE("UnregisterPrivateWindowListener listener is nullptr.");
1054        return DMError::DM_ERROR_NULLPTR;
1055    }
1056    return pImpl_->UnregisterPrivateWindowListener(listener);
1057}
1058
1059DMError DisplayManager::RegisterPrivateWindowListChangeListener(sptr<IPrivateWindowListChangeListener> listener)
1060{
1061    if (listener == nullptr) {
1062        WLOGFE("RegisterPrivateWindowListChangeListener listener is nullptr.");
1063        return DMError::DM_ERROR_NULLPTR;
1064    }
1065    return pImpl_->RegisterPrivateWindowListChangeListener(listener);
1066}
1067
1068DMError DisplayManager::UnregisterPrivateWindowListChangeListener(sptr<IPrivateWindowListChangeListener> listener)
1069{
1070    if (listener == nullptr) {
1071        WLOGFE("UnregisterPrivateWindowListChangeListener listener is nullptr.");
1072        return DMError::DM_ERROR_NULLPTR;
1073    }
1074    return pImpl_->UnregisterPrivateWindowListChangeListener(listener);
1075}
1076
1077DMError DisplayManager::Impl::RegisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)
1078{
1079    std::lock_guard<std::recursive_mutex> lock(mutex_);
1080    DMError ret = DMError::DM_OK;
1081    if (privateWindowListenerAgent_ == nullptr) {
1082        privateWindowListenerAgent_ = new DisplayManagerPrivateWindowAgent(this);
1083        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1084            privateWindowListenerAgent_,
1085            DisplayManagerAgentType::PRIVATE_WINDOW_LISTENER);
1086    }
1087    if (ret != DMError::DM_OK) {
1088        WLOGFW("RegisterDisplayManagerAgent failed !");
1089        privateWindowListenerAgent_ = nullptr;
1090    } else {
1091        WLOGI("privateWindowListener register success");
1092        privateWindowListeners_.insert(listener);
1093    }
1094    return ret;
1095}
1096
1097DMError DisplayManager::Impl::UnregisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)
1098{
1099    std::lock_guard<std::recursive_mutex> lock(mutex_);
1100    auto iter = std::find(privateWindowListeners_.begin(), privateWindowListeners_.end(), listener);
1101    if (iter == privateWindowListeners_.end()) {
1102        WLOGFE("could not find this listener");
1103        return DMError::DM_ERROR_NULLPTR;
1104    }
1105    privateWindowListeners_.erase(iter);
1106    DMError ret = DMError::DM_OK;
1107    if (privateWindowListeners_.empty() && privateWindowListenerAgent_ != nullptr) {
1108        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1109            privateWindowListenerAgent_,
1110            DisplayManagerAgentType::PRIVATE_WINDOW_LISTENER);
1111        privateWindowListenerAgent_ = nullptr;
1112    }
1113    return ret;
1114}
1115
1116DMError DisplayManager::Impl::RegisterPrivateWindowListChangeListener(sptr<IPrivateWindowListChangeListener> listener)
1117{
1118    std::lock_guard<std::recursive_mutex> lock(mutex_);
1119    DMError ret = DMError::DM_OK;
1120    if (privateWindowListChangeListenerAgent_ == nullptr) {
1121        privateWindowListChangeListenerAgent_ = new DisplayManagerPrivateWindowListAgent(this);
1122        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1123            privateWindowListChangeListenerAgent_,
1124            DisplayManagerAgentType::PRIVATE_WINDOW_LIST_LISTENER);
1125    }
1126    if (ret != DMError::DM_OK) {
1127        WLOGFW("RegisterDisplayManagerAgent failed !");
1128        privateWindowListChangeListenerAgent_ = nullptr;
1129    } else {
1130        WLOGI("privateWindowListChangeListener register success");
1131        privateWindowListChangeListeners_.insert(listener);
1132    }
1133    return ret;
1134}
1135
1136DMError DisplayManager::Impl::UnregisterPrivateWindowListChangeListener(sptr<IPrivateWindowListChangeListener> listener)
1137{
1138    std::lock_guard<std::recursive_mutex> lock(mutex_);
1139    auto iter = std::find(privateWindowListChangeListeners_.begin(), privateWindowListChangeListeners_.end(), listener);
1140    if (iter == privateWindowListChangeListeners_.end()) {
1141        WLOGFE("could not find this listener");
1142        return DMError::DM_ERROR_NULLPTR;
1143    }
1144    privateWindowListChangeListeners_.erase(iter);
1145    DMError ret = DMError::DM_OK;
1146    if (privateWindowListChangeListeners_.empty() && privateWindowListChangeListenerAgent_ != nullptr) {
1147        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1148            privateWindowListChangeListenerAgent_,
1149            DisplayManagerAgentType::PRIVATE_WINDOW_LIST_LISTENER);
1150        privateWindowListChangeListenerAgent_ = nullptr;
1151    }
1152    return ret;
1153}
1154
1155DMError DisplayManager::RegisterDisplayListener(sptr<IDisplayListener> listener)
1156{
1157    if (listener == nullptr) {
1158        WLOGFE("RegisterDisplayListener listener is nullptr.");
1159        return DMError::DM_ERROR_NULLPTR;
1160    }
1161    return pImpl_->RegisterDisplayListener(listener);
1162}
1163
1164DMError DisplayManager::Impl::UnregisterDisplayListener(sptr<IDisplayListener> listener)
1165{
1166    std::lock_guard<std::recursive_mutex> lock(mutex_);
1167    auto iter = std::find(displayListeners_.begin(), displayListeners_.end(), listener);
1168    if (iter == displayListeners_.end()) {
1169        WLOGFE("could not find this listener");
1170        return DMError::DM_ERROR_NULLPTR;
1171    }
1172    displayListeners_.erase(iter);
1173    DMError ret = DMError::DM_OK;
1174    if (displayListeners_.empty() && displayManagerListener_ != nullptr) {
1175        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1176            displayManagerListener_,
1177            DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
1178        displayManagerListener_ = nullptr;
1179    }
1180    return ret;
1181}
1182
1183DMError DisplayManager::UnregisterDisplayListener(sptr<IDisplayListener> listener)
1184{
1185    if (listener == nullptr) {
1186        WLOGFW("UnregisterDisplayListener listener is nullptr.");
1187        return DMError::DM_ERROR_NULLPTR;
1188    }
1189    return pImpl_->UnregisterDisplayListener(listener);
1190}
1191
1192DMError DisplayManager::Impl::RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
1193{
1194    std::lock_guard<std::recursive_mutex> lock(mutex_);
1195    DMError ret = DMError::DM_OK;
1196    if (powerEventListenerAgent_ == nullptr) {
1197        powerEventListenerAgent_ = new DisplayManagerAgent(this);
1198        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1199            powerEventListenerAgent_,
1200            DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
1201    }
1202    if (ret != DMError::DM_OK) {
1203        WLOGFW("RegisterDisplayManagerAgent failed !");
1204        powerEventListenerAgent_ = nullptr;
1205    } else {
1206        powerEventListeners_.insert(listener);
1207    }
1208    WLOGFD("RegisterDisplayPowerEventListener end");
1209    return ret;
1210}
1211
1212DMError DisplayManager::RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
1213{
1214    if (listener == nullptr) {
1215        WLOGFE("listener is nullptr");
1216        return DMError::DM_ERROR_NULLPTR;
1217    }
1218    return pImpl_->RegisterDisplayPowerEventListener(listener);
1219}
1220
1221DMError DisplayManager::Impl::UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
1222{
1223    std::lock_guard<std::recursive_mutex> lock(mutex_);
1224    auto iter = std::find(powerEventListeners_.begin(), powerEventListeners_.end(), listener);
1225    if (iter == powerEventListeners_.end()) {
1226        WLOGFE("could not find this listener");
1227        return DMError::DM_ERROR_NULLPTR;
1228    }
1229    powerEventListeners_.erase(iter);
1230    DMError ret = DMError::DM_OK;
1231    if (powerEventListeners_.empty() && powerEventListenerAgent_ != nullptr) {
1232        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1233            powerEventListenerAgent_,
1234            DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
1235        powerEventListenerAgent_ = nullptr;
1236    }
1237    WLOGFD("UnregisterDisplayPowerEventListener end");
1238    return ret;
1239}
1240
1241DMError DisplayManager::UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
1242{
1243    if (listener == nullptr) {
1244        WLOGFE("listener is nullptr");
1245        return DMError::DM_ERROR_NULLPTR;
1246    }
1247    return pImpl_->UnregisterDisplayPowerEventListener(listener);
1248}
1249
1250DMError DisplayManager::Impl::RegisterScreenshotListener(sptr<IScreenshotListener> listener)
1251{
1252    std::lock_guard<std::recursive_mutex> lock(mutex_);
1253    DMError ret = DMError::DM_OK;
1254    if (screenshotListenerAgent_ == nullptr) {
1255        screenshotListenerAgent_ = new DisplayManagerScreenshotAgent(this);
1256        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1257            screenshotListenerAgent_,
1258            DisplayManagerAgentType::SCREENSHOT_EVENT_LISTENER);
1259    }
1260    if (ret != DMError::DM_OK) {
1261        WLOGFW("RegisterDisplayManagerAgent failed !");
1262        screenshotListenerAgent_ = nullptr;
1263    } else {
1264        screenshotListeners_.insert(listener);
1265    }
1266    return ret;
1267}
1268
1269DMError DisplayManager::RegisterScreenshotListener(sptr<IScreenshotListener> listener)
1270{
1271    if (listener == nullptr) {
1272        WLOGFE("RegisterScreenshotListener listener is nullptr.");
1273        return DMError::DM_ERROR_NULLPTR;
1274    }
1275    return pImpl_->RegisterScreenshotListener(listener);
1276}
1277
1278DMError DisplayManager::Impl::UnregisterScreenshotListener(sptr<IScreenshotListener> listener)
1279{
1280    std::lock_guard<std::recursive_mutex> lock(mutex_);
1281    auto iter = std::find(screenshotListeners_.begin(), screenshotListeners_.end(), listener);
1282    if (iter == screenshotListeners_.end()) {
1283        WLOGFE("could not find this listener");
1284        return DMError::DM_ERROR_NULLPTR;
1285    }
1286    screenshotListeners_.erase(iter);
1287    DMError ret = DMError::DM_OK;
1288    if (screenshotListeners_.empty() && screenshotListenerAgent_ != nullptr) {
1289        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1290            screenshotListenerAgent_,
1291            DisplayManagerAgentType::SCREENSHOT_EVENT_LISTENER);
1292        screenshotListenerAgent_ = nullptr;
1293    }
1294    return ret;
1295}
1296
1297DMError DisplayManager::UnregisterScreenshotListener(sptr<IScreenshotListener> listener)
1298{
1299    if (listener == nullptr) {
1300        WLOGFE("UnregisterScreenshotListener listener is nullptr.");
1301        return DMError::DM_ERROR_NULLPTR;
1302    }
1303    return pImpl_->UnregisterScreenshotListener(listener);
1304}
1305
1306void DisplayManager::Impl::NotifyFoldAngleChanged(std::vector<float> foldAngles)
1307{
1308    std::set<sptr<IFoldAngleListener>> foldAngleListeners;
1309    {
1310        std::lock_guard<std::recursive_mutex> lock(mutex_);
1311        foldAngleListeners = foldAngleListeners_;
1312    }
1313    for (auto& listener : foldAngleListeners) {
1314        listener->OnFoldAngleChanged(foldAngles);
1315    }
1316}
1317
1318DMError DisplayManager::RegisterFoldAngleListener(sptr<IFoldAngleListener> listener)
1319{
1320    if (listener == nullptr) {
1321        WLOGFE("IFoldAngleListener listener is nullptr.");
1322        return DMError::DM_ERROR_NULLPTR;
1323    }
1324    return pImpl_->RegisterFoldAngleListener(listener);
1325}
1326
1327DMError DisplayManager::Impl::RegisterFoldAngleListener(sptr<IFoldAngleListener> listener)
1328{
1329    std::lock_guard<std::recursive_mutex> lock(mutex_);
1330    DMError ret = DMError::DM_OK;
1331    if (foldAngleListenerAgent_ == nullptr) {
1332        foldAngleListenerAgent_ = new DisplayManagerFoldAngleAgent(this);
1333        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1334            foldAngleListenerAgent_,
1335            DisplayManagerAgentType::FOLD_ANGLE_CHANGED_LISTENER);
1336    }
1337    if (ret != DMError::DM_OK) {
1338        WLOGFW("RegisterFoldAngleListener failed !");
1339        foldAngleListenerAgent_ = nullptr;
1340    } else {
1341        WLOGD("IFoldAngleListener register success");
1342        foldAngleListeners_.insert(listener);
1343    }
1344    return ret;
1345}
1346
1347DMError DisplayManager::UnregisterFoldAngleListener(sptr<IFoldAngleListener> listener)
1348{
1349    if (listener == nullptr) {
1350        WLOGFE("UnregisterFoldAngleListener listener is nullptr.");
1351        return DMError::DM_ERROR_NULLPTR;
1352    }
1353    return pImpl_->UnregisterFoldAngleListener(listener);
1354}
1355
1356DMError DisplayManager::Impl::UnregisterFoldAngleListener(sptr<IFoldAngleListener> listener)
1357{
1358    std::lock_guard<std::recursive_mutex> lock(mutex_);
1359    auto iter = std::find(foldAngleListeners_.begin(), foldAngleListeners_.end(), listener);
1360    if (iter == foldAngleListeners_.end()) {
1361        WLOGFE("could not find this listener");
1362        return DMError::DM_ERROR_NULLPTR;
1363    }
1364    foldAngleListeners_.erase(iter);
1365    DMError ret = DMError::DM_OK;
1366    if (foldAngleListeners_.empty() && foldAngleListenerAgent_ != nullptr) {
1367        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1368            foldAngleListenerAgent_,
1369            DisplayManagerAgentType::FOLD_ANGLE_CHANGED_LISTENER);
1370        foldAngleListenerAgent_ = nullptr;
1371    }
1372    return ret;
1373}
1374
1375void DisplayManager::Impl::NotifyCaptureStatusChanged(bool isCapture)
1376{
1377    std::set<sptr<ICaptureStatusListener>> captureStatusListeners;
1378    {
1379        std::lock_guard<std::recursive_mutex> lock(mutex_);
1380        captureStatusListeners = captureStatusListeners_;
1381    }
1382    for (auto& listener : captureStatusListeners) {
1383        listener->OnCaptureStatusChanged(isCapture);
1384    }
1385}
1386
1387DMError DisplayManager::RegisterCaptureStatusListener(sptr<ICaptureStatusListener> listener)
1388{
1389    if (listener == nullptr) {
1390        WLOGFE("ICaptureStatusListener listener is nullptr.");
1391        return DMError::DM_ERROR_NULLPTR;
1392    }
1393    return pImpl_->RegisterCaptureStatusListener(listener);
1394}
1395
1396DMError DisplayManager::Impl::RegisterCaptureStatusListener(sptr<ICaptureStatusListener> listener)
1397{
1398    std::lock_guard<std::recursive_mutex> lock(mutex_);
1399    DMError ret = DMError::DM_OK;
1400    if (captureStatusListenerAgent_ == nullptr) {
1401        captureStatusListenerAgent_ = new DisplayManagerCaptureStatusAgent(this);
1402        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1403            captureStatusListenerAgent_,
1404            DisplayManagerAgentType::CAPTURE_STATUS_CHANGED_LISTENER);
1405    }
1406    if (ret != DMError::DM_OK) {
1407        WLOGFE("RegisterCaptureStatusListener failed !");
1408        captureStatusListenerAgent_ = nullptr;
1409    } else {
1410        WLOGD("ICaptureStatusListener register success");
1411        captureStatusListeners_.insert(listener);
1412    }
1413    return ret;
1414}
1415
1416DMError DisplayManager::UnregisterCaptureStatusListener(sptr<ICaptureStatusListener> listener)
1417{
1418    if (listener == nullptr) {
1419        WLOGFE("UnregisterCaptureStatusListener listener is nullptr.");
1420        return DMError::DM_ERROR_NULLPTR;
1421    }
1422    return pImpl_->UnregisterCaptureStatusListener(listener);
1423}
1424
1425DMError DisplayManager::Impl::UnregisterCaptureStatusListener(sptr<ICaptureStatusListener> listener)
1426{
1427    std::lock_guard<std::recursive_mutex> lock(mutex_);
1428    auto iter = std::find(captureStatusListeners_.begin(), captureStatusListeners_.end(), listener);
1429    if (iter == captureStatusListeners_.end()) {
1430        WLOGFE("could not find this listener");
1431        return DMError::DM_ERROR_NULLPTR;
1432    }
1433    captureStatusListeners_.erase(iter);
1434    DMError ret = DMError::DM_OK;
1435    if (captureStatusListeners_.empty() && captureStatusListenerAgent_ != nullptr) {
1436        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1437            captureStatusListenerAgent_,
1438            DisplayManagerAgentType::CAPTURE_STATUS_CHANGED_LISTENER);
1439        captureStatusListenerAgent_ = nullptr;
1440    }
1441    return ret;
1442}
1443
1444void DisplayManager::Impl::NotifyFoldStatusChanged(FoldStatus foldStatus)
1445{
1446    std::set<sptr<IFoldStatusListener>> foldStatusListeners;
1447    {
1448        std::lock_guard<std::recursive_mutex> lock(mutex_);
1449        foldStatusListeners = foldStatusListeners_;
1450    }
1451    for (auto& listener : foldStatusListeners) {
1452        listener->OnFoldStatusChanged(foldStatus);
1453    }
1454}
1455
1456DMError DisplayManager::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
1457{
1458    if (listener == nullptr) {
1459        WLOGFE("IFoldStatusListener listener is nullptr.");
1460        return DMError::DM_ERROR_NULLPTR;
1461    }
1462    return pImpl_->RegisterFoldStatusListener(listener);
1463}
1464
1465DMError DisplayManager::Impl::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
1466{
1467    std::lock_guard<std::recursive_mutex> lock(mutex_);
1468    DMError ret = DMError::DM_OK;
1469    if (foldStatusListenerAgent_ == nullptr) {
1470        foldStatusListenerAgent_ = new DisplayManagerFoldStatusAgent(this);
1471        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1472            foldStatusListenerAgent_,
1473            DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
1474    }
1475    if (ret != DMError::DM_OK) {
1476        WLOGFW("RegisterFoldStatusListener failed !");
1477        foldStatusListenerAgent_ = nullptr;
1478    } else {
1479        WLOGD("IFoldStatusListener register success");
1480        foldStatusListeners_.insert(listener);
1481    }
1482    return ret;
1483}
1484
1485DMError DisplayManager::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
1486{
1487    if (listener == nullptr) {
1488        WLOGFE("UnregisterFoldStatusListener listener is nullptr.");
1489        return DMError::DM_ERROR_NULLPTR;
1490    }
1491    return pImpl_->UnregisterFoldStatusListener(listener);
1492}
1493
1494DMError DisplayManager::Impl::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
1495{
1496    std::lock_guard<std::recursive_mutex> lock(mutex_);
1497    auto iter = std::find(foldStatusListeners_.begin(), foldStatusListeners_.end(), listener);
1498    if (iter == foldStatusListeners_.end()) {
1499        WLOGFE("could not find this listener");
1500        return DMError::DM_ERROR_NULLPTR;
1501    }
1502    foldStatusListeners_.erase(iter);
1503    DMError ret = DMError::DM_OK;
1504    if (foldStatusListeners_.empty() && foldStatusListenerAgent_ != nullptr) {
1505        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1506            foldStatusListenerAgent_,
1507            DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
1508        foldStatusListenerAgent_ = nullptr;
1509    }
1510    return ret;
1511}
1512
1513void DisplayManager::Impl::NotifyDisplayChangeInfoChanged(const sptr<DisplayChangeInfo>& info)
1514{
1515    std::set<sptr<IDisplayUpdateListener>> displayUpdateListeners;
1516    {
1517        std::lock_guard<std::recursive_mutex> lock(mutex_);
1518        displayUpdateListeners = displayUpdateListeners_;
1519    }
1520    for (auto& listener : displayUpdateListeners) {
1521        listener->OnDisplayUpdate(info);
1522    }
1523}
1524
1525DMError DisplayManager::RegisterDisplayUpdateListener(sptr<IDisplayUpdateListener> listener)
1526{
1527    if (listener == nullptr) {
1528        WLOGFE("IDisplayUpdateListener listener is nullptr.");
1529        return DMError::DM_ERROR_NULLPTR;
1530    }
1531    return pImpl_->RegisterDisplayUpdateListener(listener);
1532}
1533
1534DMError DisplayManager::Impl::RegisterDisplayUpdateListener(sptr<IDisplayUpdateListener> listener)
1535{
1536    std::lock_guard<std::recursive_mutex> lock(mutex_);
1537    DMError ret = DMError::DM_OK;
1538    if (displayUpdateListenerAgent_ == nullptr) {
1539        displayUpdateListenerAgent_ = new DisplayManagerDisplayUpdateAgent(this);
1540        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1541            displayUpdateListenerAgent_,
1542            DisplayManagerAgentType::DISPLAY_UPDATE_LISTENER);
1543    }
1544    if (ret != DMError::DM_OK) {
1545        WLOGFW("RegisterDisplayUpdateListener failed !");
1546        displayUpdateListenerAgent_ = nullptr;
1547    } else {
1548        WLOGI("IDisplayUpdateListener register success");
1549        displayUpdateListeners_.insert(listener);
1550    }
1551    return ret;
1552}
1553
1554DMError DisplayManager::UnregisterDisplayUpdateListener(sptr<IDisplayUpdateListener> listener)
1555{
1556    if (listener == nullptr) {
1557        WLOGFE("UnregisterDisplayUpdateListener listener is nullptr.");
1558        return DMError::DM_ERROR_NULLPTR;
1559    }
1560    return pImpl_->UnregisterDisplayUpdateListener(listener);
1561}
1562
1563DMError DisplayManager::Impl::UnregisterDisplayUpdateListener(sptr<IDisplayUpdateListener> listener)
1564{
1565    std::lock_guard<std::recursive_mutex> lock(mutex_);
1566    auto iter = std::find(displayUpdateListeners_.begin(), displayUpdateListeners_.end(), listener);
1567    if (iter == displayUpdateListeners_.end()) {
1568        WLOGFE("could not find this listener");
1569        return DMError::DM_ERROR_NULLPTR;
1570    }
1571    displayUpdateListeners_.erase(iter);
1572    DMError ret = DMError::DM_OK;
1573    if (displayUpdateListeners_.empty() && displayUpdateListenerAgent_ != nullptr) {
1574        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1575            displayUpdateListenerAgent_,
1576            DisplayManagerAgentType::DISPLAY_UPDATE_LISTENER);
1577        displayUpdateListenerAgent_ = nullptr;
1578    }
1579    return ret;
1580}
1581
1582void DisplayManager::Impl::NotifyDisplayModeChanged(FoldDisplayMode displayMode)
1583{
1584    std::set<sptr<IDisplayModeListener>> displayModeListeners;
1585    {
1586        std::lock_guard<std::recursive_mutex> lock(mutex_);
1587        displayModeListeners = displayModeListeners_;
1588    }
1589    auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
1590    NotifyDisplayChange(displayInfo);
1591    for (auto& listener : displayModeListeners) {
1592        listener->OnDisplayModeChanged(displayMode);
1593    }
1594}
1595
1596void DisplayManager::Impl::NotifyAvailableAreaChanged(DMRect rect)
1597{
1598    std::set<sptr<IAvailableAreaListener>> availableAreaListeners;
1599    {
1600        std::lock_guard<std::recursive_mutex> lock(mutex_);
1601        availableAreaListeners = availableAreaListeners_;
1602    }
1603    for (auto& listener : availableAreaListeners) {
1604        listener->OnAvailableAreaChanged(rect);
1605    }
1606}
1607
1608DMError DisplayManager::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
1609{
1610    if (listener == nullptr) {
1611        WLOGFE("IDisplayModeListener listener is nullptr.");
1612        return DMError::DM_ERROR_NULLPTR;
1613    }
1614    return pImpl_->RegisterDisplayModeListener(listener);
1615}
1616
1617DMError DisplayManager::Impl::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
1618{
1619    std::lock_guard<std::recursive_mutex> lock(mutex_);
1620    DMError ret = DMError::DM_OK;
1621    if (displayModeListenerAgent_ == nullptr) {
1622        displayModeListenerAgent_ = new DisplayManagerDisplayModeAgent(this);
1623        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1624            displayModeListenerAgent_,
1625            DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
1626    }
1627    if (ret != DMError::DM_OK) {
1628        WLOGFW("RegisterDisplayModeListener failed !");
1629        displayModeListenerAgent_ = nullptr;
1630    } else {
1631        WLOGD("IDisplayModeListener register success");
1632        displayModeListeners_.insert(listener);
1633    }
1634    return ret;
1635}
1636
1637DMError DisplayManager::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
1638{
1639    if (listener == nullptr) {
1640        WLOGFE("UnregisterPrivateWindowListener listener is nullptr.");
1641        return DMError::DM_ERROR_NULLPTR;
1642    }
1643    return pImpl_->UnregisterDisplayModeListener(listener);
1644}
1645
1646DMError DisplayManager::Impl::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
1647{
1648    std::lock_guard<std::recursive_mutex> lock(mutex_);
1649    auto iter = std::find(displayModeListeners_.begin(), displayModeListeners_.end(), listener);
1650    if (iter == displayModeListeners_.end()) {
1651        WLOGFE("could not find this listener");
1652        return DMError::DM_ERROR_NULLPTR;
1653    }
1654    displayModeListeners_.erase(iter);
1655    DMError ret = DMError::DM_OK;
1656    if (displayModeListeners_.empty() && displayModeListenerAgent_ != nullptr) {
1657        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1658            displayModeListenerAgent_,
1659            DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
1660        displayModeListenerAgent_ = nullptr;
1661    }
1662    return ret;
1663}
1664
1665DMError DisplayManager::RegisterAvailableAreaListener(sptr<IAvailableAreaListener> listener)
1666{
1667    if (listener == nullptr) {
1668        WLOGFE("RegisterAvailableAreaListener listener is nullptr.");
1669        return DMError::DM_ERROR_NULLPTR;
1670    }
1671    return pImpl_->RegisterAvailableAreaListener(listener);
1672}
1673
1674DMError DisplayManager::Impl::RegisterAvailableAreaListener(sptr<IAvailableAreaListener> listener)
1675{
1676    std::lock_guard<std::recursive_mutex> lock(mutex_);
1677    DMError ret = DMError::DM_OK;
1678    if (availableAreaListenerAgent_ == nullptr) {
1679        availableAreaListenerAgent_ = new DisplayManagerAvailableAreaAgent(this);
1680        ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1681            availableAreaListenerAgent_,
1682            DisplayManagerAgentType::AVAILABLE_AREA_CHANGED_LISTENER);
1683    }
1684    if (ret != DMError::DM_OK) {
1685        WLOGFW("RegisterAvailableAreaListener failed !");
1686        availableAreaListenerAgent_ = nullptr;
1687    } else {
1688        WLOGD("IAvailableAreaListener register success");
1689        availableAreaListeners_.insert(listener);
1690    }
1691    return ret;
1692}
1693
1694DMError DisplayManager::UnregisterAvailableAreaListener(sptr<IAvailableAreaListener> listener)
1695{
1696    if (listener == nullptr) {
1697        WLOGFE("UnregisterPrivateWindowListener listener is nullptr.");
1698        return DMError::DM_ERROR_NULLPTR;
1699    }
1700    return pImpl_->UnregisterAvailableAreaListener(listener);
1701}
1702
1703DMError DisplayManager::Impl::UnregisterAvailableAreaListener(sptr<IAvailableAreaListener> listener)
1704{
1705    std::lock_guard<std::recursive_mutex> lock(mutex_);
1706    auto iter = std::find(availableAreaListeners_.begin(), availableAreaListeners_.end(), listener);
1707    if (iter == availableAreaListeners_.end()) {
1708        WLOGFE("could not find this listener");
1709        return DMError::DM_ERROR_NULLPTR;
1710    }
1711    availableAreaListeners_.erase(iter);
1712    DMError ret = DMError::DM_OK;
1713    if (availableAreaListeners_.empty() && availableAreaListenerAgent_ != nullptr) {
1714        ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
1715            availableAreaListenerAgent_,
1716            DisplayManagerAgentType::AVAILABLE_AREA_CHANGED_LISTENER);
1717        availableAreaListenerAgent_ = nullptr;
1718    }
1719    return ret;
1720}
1721
1722void DisplayManager::Impl::NotifyScreenshot(sptr<ScreenshotInfo> info)
1723{
1724    WLOGFI("NotifyScreenshot trigger:[%{public}s] displayId:%{public}" PRIu64" size:%{public}zu",
1725        info->GetTrigger().c_str(), info->GetDisplayId(), screenshotListeners_.size());
1726    std::set<sptr<IScreenshotListener>> screenshotListeners;
1727    {
1728        std::lock_guard<std::recursive_mutex> lock(mutex_);
1729        screenshotListeners = screenshotListeners_;
1730    }
1731    for (auto& listener : screenshotListeners) {
1732        listener->OnScreenshot(*info);
1733    }
1734}
1735
1736void DisplayManager::Impl::NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status)
1737{
1738    WLOGFD("[UL_POWER]NotifyDisplayPowerEvent event:%{public}u, status:%{public}u, size:%{public}zu", event, status,
1739        powerEventListeners_.size());
1740    std::set<sptr<IDisplayPowerEventListener>> powerEventListeners;
1741    {
1742        std::lock_guard<std::recursive_mutex> lock(mutex_);
1743        powerEventListeners = powerEventListeners_;
1744    }
1745    for (auto& listener : powerEventListeners) {
1746        listener->OnDisplayPowerEvent(event, status);
1747    }
1748}
1749
1750void DisplayManager::Impl::NotifyDisplayStateChanged(DisplayId id, DisplayState state)
1751{
1752    WLOGFD("state:%{public}u", state);
1753    DisplayStateCallback displayStateCallback = nullptr;
1754    {
1755        std::lock_guard<std::recursive_mutex> lock(mutex_);
1756        displayStateCallback = displayStateCallback_;
1757    }
1758    if (displayStateCallback) {
1759        displayStateCallback(state);
1760        ClearDisplayStateCallback();
1761        return;
1762    }
1763    WLOGFW("callback_ target is not set!");
1764}
1765
1766void DisplayManager::Impl::NotifyDisplayCreate(sptr<DisplayInfo> info)
1767{
1768    std::lock_guard<std::recursive_mutex> lock(mutex_);
1769    UpdateDisplayInfoLocked(info);
1770}
1771
1772void DisplayManager::Impl::NotifyDisplayDestroy(DisplayId displayId)
1773{
1774    WLOGFD("displayId:%{public}" PRIu64".", displayId);
1775    std::lock_guard<std::recursive_mutex> lock(mutex_);
1776    displayMap_.erase(displayId);
1777}
1778
1779void DisplayManager::Impl::NotifyDisplayChange(sptr<DisplayInfo> displayInfo)
1780{
1781    std::lock_guard<std::recursive_mutex> lock(mutex_);
1782    UpdateDisplayInfoLocked(displayInfo);
1783}
1784
1785bool DisplayManager::Impl::UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)
1786{
1787    if (displayInfo == nullptr) {
1788        WLOGFW("displayInfo is null");
1789        return false;
1790    }
1791    DisplayId displayId = displayInfo->GetDisplayId();
1792    WLOGFD("displayId:%{public}" PRIu64".", displayId);
1793    if (displayId == DISPLAY_ID_INVALID) {
1794        WLOGFE("displayId is invalid.");
1795        return false;
1796    }
1797    auto iter = displayMap_.find(displayId);
1798    if (iter != displayMap_.end() && iter->second != nullptr) {
1799        WLOGFD("display Info Updated: %{public}s",
1800            GetDisplayInfoSrting(displayInfo).c_str());
1801        iter->second->UpdateDisplayInfo(displayInfo);
1802        return true;
1803    }
1804    sptr<Display> display = new (std::nothrow) Display("", displayInfo);
1805    if (display == nullptr) {
1806        WLOGFE("malloc display failed");
1807        return false;
1808    }
1809    displayMap_[displayId] = display;
1810    return true;
1811}
1812
1813std::string DisplayManager::Impl::GetDisplayInfoSrting(sptr<DisplayInfo> displayInfo)
1814{
1815    if (displayInfo == nullptr) {
1816        WLOGFE("displayInfo nullptr.");
1817        return "";
1818    }
1819    std::ostringstream oss;
1820    oss <<  "Display ID: " << displayInfo->GetDisplayId() << ", ";
1821    oss <<  "Name: " << displayInfo->GetName() << ", ";
1822    oss <<  "RefreshRate: " << displayInfo->GetRefreshRate() << ", ";
1823    oss <<  "VirtualPixelRatio: " << displayInfo->GetVirtualPixelRatio() << ", ";
1824    oss <<  "DensityInCurResolution: " << displayInfo->GetDensityInCurResolution() << ", ";
1825    oss <<  "DefaultVirtualPixelRatio: " << displayInfo->GetDefaultVirtualPixelRatio() << ", ";
1826    oss <<  "Rotation: " << static_cast<int32_t>(displayInfo->GetRotation());
1827    return oss.str();
1828}
1829
1830bool DisplayManager::WakeUpBegin(PowerStateChangeReason reason)
1831{
1832    WLOGFD("[UL_POWER]WakeUpBegin start, reason:%{public}u", reason);
1833    return SingletonContainer::Get<DisplayManagerAdapter>().WakeUpBegin(reason);
1834}
1835
1836bool DisplayManager::WakeUpEnd()
1837{
1838    WLOGFD("[UL_POWER]WakeUpEnd start");
1839    return SingletonContainer::Get<DisplayManagerAdapter>().WakeUpEnd();
1840}
1841
1842bool DisplayManager::SuspendBegin(PowerStateChangeReason reason)
1843{
1844    // dms->wms notify other windows to hide
1845    WLOGFD("[UL_POWER]SuspendBegin start, reason:%{public}u", reason);
1846    return SingletonContainer::Get<DisplayManagerAdapter>().SuspendBegin(reason);
1847}
1848
1849bool DisplayManager::SuspendEnd()
1850{
1851    WLOGFD("[UL_POWER]SuspendEnd start");
1852    return SingletonContainer::Get<DisplayManagerAdapter>().SuspendEnd();
1853}
1854
1855ScreenId DisplayManager::GetInternalScreenId()
1856{
1857    WLOGFD("[UL_POWER]GetInternalScreenId start");
1858    return SingletonContainer::Get<DisplayManagerAdapter>().GetInternalScreenId();
1859}
1860
1861bool DisplayManager::SetScreenPowerById(ScreenId screenId, ScreenPowerState state, PowerStateChangeReason reason)
1862{
1863    WLOGFD("[UL_POWER]SetScreenPowerById start");
1864    return SingletonContainer::Get<DisplayManagerAdapter>().SetScreenPowerById(screenId, state, reason);
1865}
1866
1867bool DisplayManager::Impl::SetDisplayState(DisplayState state, DisplayStateCallback callback)
1868{
1869    WLOGFD("[UL_POWER]state:%{public}u", state);
1870    bool ret = true;
1871    {
1872        std::lock_guard<std::recursive_mutex> lock(mutex_);
1873        if (displayStateCallback_ != nullptr || callback == nullptr) {
1874            WLOGFI("[UL_POWER]previous callback not called or callback invalid");
1875            if (displayStateCallback_ != nullptr) {
1876                WLOGFI("[UL_POWER]previous callback not called, the displayStateCallback_ is not null");
1877            }
1878            if (callback == nullptr) {
1879                WLOGFI("[UL_POWER]Invalid callback received");
1880            }
1881            return false;
1882        }
1883        displayStateCallback_ = callback;
1884
1885        if (displayStateAgent_ == nullptr) {
1886            displayStateAgent_ = new DisplayManagerAgent(this);
1887            ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1888                displayStateAgent_,
1889                DisplayManagerAgentType::DISPLAY_STATE_LISTENER) == DMError::DM_OK;
1890        }
1891    }
1892    ret = ret && SingletonContainer::Get<DisplayManagerAdapter>().SetDisplayState(state);
1893    if (!ret) {
1894        ClearDisplayStateCallback();
1895    }
1896    return ret;
1897}
1898
1899bool DisplayManager::SetDisplayState(DisplayState state, DisplayStateCallback callback)
1900{
1901    return pImpl_->SetDisplayState(state, callback);
1902}
1903
1904DisplayState DisplayManager::GetDisplayState(DisplayId displayId)
1905{
1906    return SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayState(displayId);
1907}
1908
1909bool DisplayManager::TryToCancelScreenOff()
1910{
1911    WLOGFD("[UL_POWER]TryToCancelScreenOff start");
1912    return SingletonContainer::Get<DisplayManagerAdapter>().TryToCancelScreenOff();
1913}
1914
1915bool DisplayManager::SetScreenBrightness(uint64_t screenId, uint32_t level)
1916{
1917    WLOGFI("[UL_POWER]ScreenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
1918    RSInterfaces::GetInstance().SetScreenBacklight(screenId, level);
1919    return true;
1920}
1921
1922uint32_t DisplayManager::GetScreenBrightness(uint64_t screenId) const
1923{
1924    uint32_t level = static_cast<uint32_t>(RSInterfaces::GetInstance().GetScreenBacklight(screenId));
1925    WLOGFI("GetScreenBrightness screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
1926    return level;
1927}
1928
1929void DisplayManager::NotifyDisplayEvent(DisplayEvent event)
1930{
1931    // Unlock event dms->wms restore other hidden windows
1932    WLOGFD("[UL_POWER]DisplayEvent:%{public}u", event);
1933    SingletonContainer::Get<DisplayManagerAdapter>().NotifyDisplayEvent(event);
1934}
1935
1936bool DisplayManager::Freeze(std::vector<DisplayId> displayIds)
1937{
1938    WLOGFD("freeze display");
1939    if (displayIds.size() == 0) {
1940        WLOGFE("freeze display fail, num of display is 0");
1941        return false;
1942    }
1943    if (displayIds.size() > MAX_DISPLAY_SIZE) {
1944        WLOGFE("freeze display fail, displayIds size is bigger than %{public}u.", MAX_DISPLAY_SIZE);
1945        return false;
1946    }
1947    return SingletonContainer::Get<DisplayManagerAdapter>().SetFreeze(displayIds, true);
1948}
1949
1950bool DisplayManager::Unfreeze(std::vector<DisplayId> displayIds)
1951{
1952    WLOGFD("unfreeze display");
1953    if (displayIds.size() == 0) {
1954        WLOGFE("unfreeze display fail, num of display is 0");
1955        return false;
1956    }
1957    if (displayIds.size() > MAX_DISPLAY_SIZE) {
1958        WLOGFE("unfreeze display fail, displayIds size is bigger than %{public}u.", MAX_DISPLAY_SIZE);
1959        return false;
1960    }
1961    return SingletonContainer::Get<DisplayManagerAdapter>().SetFreeze(displayIds, false);
1962}
1963
1964DMError DisplayManager::AddSurfaceNodeToDisplay(DisplayId displayId,
1965    std::shared_ptr<class RSSurfaceNode>& surfaceNode)
1966{
1967    return SingletonContainer::Get<DisplayManagerAdapter>().AddSurfaceNodeToDisplay(displayId, surfaceNode);
1968}
1969
1970DMError DisplayManager::RemoveSurfaceNodeFromDisplay(DisplayId displayId,
1971    std::shared_ptr<class RSSurfaceNode>& surfaceNode)
1972{
1973    return SingletonContainer::Get<DisplayManagerAdapter>().RemoveSurfaceNodeFromDisplay(displayId, surfaceNode);
1974}
1975
1976void DisplayManager::Impl::OnRemoteDied()
1977{
1978    WLOGFI("dms is died");
1979    if (g_dmIsDestroyed) {
1980        WLOGFE("dm has been destructed, mutex_ is invalid");
1981        return;
1982    }
1983    std::lock_guard<std::recursive_mutex> lock(mutex_);
1984    displayManagerListener_ = nullptr;
1985    displayStateAgent_ = nullptr;
1986    powerEventListenerAgent_ = nullptr;
1987    screenshotListenerAgent_ = nullptr;
1988    privateWindowListenerAgent_ = nullptr;
1989    privateWindowListChangeListenerAgent_ = nullptr;
1990    foldStatusListenerAgent_ = nullptr;
1991    foldAngleListenerAgent_ = nullptr;
1992    captureStatusListenerAgent_ = nullptr;
1993}
1994
1995void DisplayManager::OnRemoteDied()
1996{
1997    if (g_dmIsDestroyed) {
1998        WLOGFE("dms is dying, pImpl_ is nullptr");
1999        return;
2000    }
2001    pImpl_->OnRemoteDied();
2002}
2003
2004bool DisplayManager::ConvertScreenIdToRsScreenId(ScreenId screenId, ScreenId& rsScreenId)
2005{
2006    return pImpl_->ConvertScreenIdToRsScreenId(screenId, rsScreenId);
2007}
2008
2009bool DisplayManager::Impl::ConvertScreenIdToRsScreenId(ScreenId screenId, ScreenId& rsScreenId)
2010{
2011    bool res = SingletonContainer::Get<DisplayManagerAdapter>().ConvertScreenIdToRsScreenId(screenId, rsScreenId);
2012    WLOGFD("Convert ScreenId %{public}" PRIu64" To RsScreenId %{public}" PRIu64"", screenId, rsScreenId);
2013    return res;
2014}
2015
2016void DisplayManager::SetVirtualScreenBlackList(ScreenId screenId, std::vector<uint64_t>& windowIdList)
2017{
2018    SingletonContainer::Get<DisplayManagerAdapter>().SetVirtualScreenBlackList(screenId, windowIdList);
2019}
2020
2021void DisplayManager::DisablePowerOffRenderControl(ScreenId screenId)
2022{
2023    SingletonContainer::Get<DisplayManagerAdapter>().DisablePowerOffRenderControl(screenId);
2024}
2025
2026DMError DisplayManager::ProxyForFreeze(std::set<int32_t> pidList, bool isProxy)
2027{
2028    std::ostringstream oss;
2029    for (auto pid : pidList) {
2030        oss << pid << " ";
2031    }
2032    WLOGFI("pidList:%{public}s, isProxy: %{public}d", oss.str().c_str(), isProxy);
2033    return pImpl_->ProxyForFreeze(pidList, isProxy);
2034}
2035
2036DMError DisplayManager::Impl::ProxyForFreeze(const std::set<int32_t>& pidList, bool isProxy)
2037{
2038    return SingletonContainer::Get<DisplayManagerAdapter>().ProxyForFreeze(pidList, isProxy);
2039}
2040
2041DMError DisplayManager::ResetAllFreezeStatus()
2042{
2043    return pImpl_->ResetAllFreezeStatus();
2044}
2045
2046DMError DisplayManager::Impl::ResetAllFreezeStatus()
2047{
2048    return SingletonContainer::Get<DisplayManagerAdapter>().ResetAllFreezeStatus();
2049}
2050
2051DMError DisplayManager::SetVirtualScreenSecurityExemption(ScreenId screenId, uint32_t pid,
2052    std::vector<uint64_t>& windowIdList)
2053{
2054    return pImpl_->SetVirtualScreenSecurityExemption(screenId, pid, windowIdList);
2055}
2056
2057DMError DisplayManager::Impl::SetVirtualScreenSecurityExemption(ScreenId screenId, uint32_t pid,
2058    std::vector<uint64_t>& windowIdList)
2059{
2060    return SingletonContainer::Get<DisplayManagerAdapter>().SetVirtualScreenSecurityExemption(
2061        screenId, pid, windowIdList);
2062}
2063} // namespace OHOS::Rosen
2064
2065