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