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