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