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