• 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_DISPLAY_SIZE = 32;
34     const static uint32_t MAX_INTERVAL_US = 5000;
35 }
36 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManager)
37 
38 class DisplayManager::Impl : public RefBase {
39 public:
Impl(std::recursive_mutex & mutex)40     Impl(std::recursive_mutex& mutex) : mutex_(mutex) {}
41     ~Impl();
42     static inline SingletonDelegator<DisplayManager> delegator;
43     bool CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const;
44     bool CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const;
45     sptr<Display> GetDefaultDisplay();
46     sptr<Display> GetDefaultDisplaySync();
47     sptr<Display> GetDisplayById(DisplayId displayId);
48     DMError HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow);
49 
50     bool IsFoldable();
51 
52     FoldStatus GetFoldStatus();
53 
54     FoldDisplayMode GetFoldDisplayMode();
55 
56     void SetFoldDisplayMode(const FoldDisplayMode);
57 
58     sptr<FoldCreaseRegion> GetCurrentFoldCreaseRegion();
59 
60     DMError RegisterDisplayListener(sptr<IDisplayListener> listener);
61     DMError UnregisterDisplayListener(sptr<IDisplayListener> listener);
62     bool SetDisplayState(DisplayState state, DisplayStateCallback callback);
63     DMError RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener);
64     DMError UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener);
65     DMError RegisterScreenshotListener(sptr<IScreenshotListener> listener);
66     DMError UnregisterScreenshotListener(sptr<IScreenshotListener> listener);
67     DMError RegisterPrivateWindowListener(sptr<IPrivateWindowListener> listener);
68     DMError UnregisterPrivateWindowListener(sptr<IPrivateWindowListener> listener);
69     DMError RegisterFoldStatusListener(sptr<IFoldStatusListener> listener);
70     DMError UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener);
71     DMError RegisterDisplayModeListener(sptr<IDisplayModeListener> listener);
72     DMError UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener);
73     sptr<Display> GetDisplayByScreenId(ScreenId screenId);
74     void OnRemoteDied();
75 private:
76     void ClearDisplayStateCallback();
77     void NotifyPrivateWindowStateChanged(bool hasPrivate);
78     void NotifyScreenshot(sptr<ScreenshotInfo> info);
79     void NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status);
80     void NotifyDisplayStateChanged(DisplayId id, DisplayState state);
81     void NotifyDisplayChangedEvent(sptr<DisplayInfo> info, DisplayChangeEvent event);
82     void NotifyDisplayCreate(sptr<DisplayInfo> info);
83     void NotifyDisplayDestroy(DisplayId);
84     void NotifyDisplayChange(sptr<DisplayInfo> displayInfo);
85     bool UpdateDisplayInfoLocked(sptr<DisplayInfo>);
86     void NotifyFoldStatusChanged(FoldStatus foldStatus);
87     void NotifyDisplayModeChanged(FoldDisplayMode displayMode);
88     void Clear();
89 
90     DisplayId defaultDisplayId_ = DISPLAY_ID_INVALID;
91     std::map<DisplayId, sptr<Display>> displayMap_;
92     DisplayStateCallback displayStateCallback_;
93     std::recursive_mutex& mutex_;
94     std::set<sptr<IDisplayListener>> displayListeners_;
95     std::set<sptr<IDisplayPowerEventListener>> powerEventListeners_;
96     std::set<sptr<IScreenshotListener>> screenshotListeners_;
97     std::set<sptr<IPrivateWindowListener>> privateWindowListeners_;
98     std::set<sptr<IFoldStatusListener>> foldStatusListeners_;
99     std::set<sptr<IDisplayModeListener>> displayModeListeners_;
100     class DisplayManagerListener;
101     sptr<DisplayManagerListener> displayManagerListener_;
102     class DisplayManagerAgent;
103     sptr<DisplayManagerAgent> displayStateAgent_;
104     sptr<DisplayManagerAgent> powerEventListenerAgent_;
105     class DisplayManagerScreenshotAgent;
106     sptr<DisplayManagerScreenshotAgent> screenshotListenerAgent_;
107     class DisplayManagerPrivateWindowAgent;
108     sptr<DisplayManagerPrivateWindowAgent> privateWindowListenerAgent_;
109     class DisplayManagerFoldStatusAgent;
110     sptr<DisplayManagerFoldStatusAgent> foldStatusListenerAgent_;
111     class DisplayManagerDisplayModeAgent;
112     sptr<DisplayManagerDisplayModeAgent> displayModeListenerAgent_;
113 };
114 
115 class DisplayManager::Impl::DisplayManagerListener : public DisplayManagerAgentDefault {
116 public:
DisplayManagerListener(sptr<Impl> impl)117     explicit DisplayManagerListener(sptr<Impl> impl) : pImpl_(impl)
118     {
119     }
120 
OnDisplayCreate(sptr<DisplayInfo> displayInfo)121     void OnDisplayCreate(sptr<DisplayInfo> displayInfo) override
122     {
123         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
124             WLOGFE("OnDisplayCreate, displayInfo is invalid.");
125             return;
126         }
127         if (pImpl_ == nullptr) {
128             WLOGFE("OnDisplayCreate, impl is nullptr.");
129             return;
130         }
131         pImpl_->NotifyDisplayCreate(displayInfo);
132         std::set<sptr<IDisplayListener>> displayListeners;
133         {
134             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
135             displayListeners = pImpl_->displayListeners_;
136         }
137         for (auto listener : displayListeners) {
138             listener->OnCreate(displayInfo->GetDisplayId());
139         }
140     };
141 
OnDisplayDestroy(DisplayId displayId)142     void OnDisplayDestroy(DisplayId displayId) override
143     {
144         if (displayId == DISPLAY_ID_INVALID) {
145             WLOGFE("OnDisplayDestroy, displayId is invalid.");
146             return;
147         }
148         if (pImpl_ == nullptr) {
149             WLOGFE("OnDisplayDestroy, impl is nullptr.");
150             return;
151         }
152         pImpl_->NotifyDisplayDestroy(displayId);
153         std::set<sptr<IDisplayListener>> displayListeners;
154         {
155             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
156             displayListeners = pImpl_->displayListeners_;
157         }
158         for (auto listener : displayListeners) {
159             listener->OnDestroy(displayId);
160         }
161     };
162 
OnDisplayChange(sptr<DisplayInfo> displayInfo,DisplayChangeEvent event)163     void OnDisplayChange(sptr<DisplayInfo> displayInfo, DisplayChangeEvent event) override
164     {
165         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
166             WLOGFE("OnDisplayChange, displayInfo is invalid.");
167             return;
168         }
169         if (pImpl_ == nullptr) {
170             WLOGFE("OnDisplayChange, impl is nullptr.");
171             return;
172         }
173         WLOGD("OnDisplayChange. display %{public}" PRIu64", event %{public}u", displayInfo->GetDisplayId(), event);
174         pImpl_->NotifyDisplayChange(displayInfo);
175         std::set<sptr<IDisplayListener>> displayListeners;
176         {
177             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
178             displayListeners = pImpl_->displayListeners_;
179         }
180         for (auto listener : displayListeners) {
181             listener->OnChange(displayInfo->GetDisplayId());
182         }
183     };
184 private:
185     sptr<Impl> pImpl_;
186 };
187 
188 class DisplayManager::Impl::DisplayManagerAgent : public DisplayManagerAgentDefault {
189 public:
DisplayManagerAgent(sptr<Impl> impl)190     explicit DisplayManagerAgent(sptr<Impl> impl) : pImpl_(impl)
191     {
192     }
193     ~DisplayManagerAgent() = default;
194 
NotifyDisplayPowerEvent(DisplayPowerEvent event,EventStatus status)195     virtual void NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) override
196     {
197         pImpl_->NotifyDisplayPowerEvent(event, status);
198     }
199 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)200     virtual void NotifyDisplayStateChanged(DisplayId id, DisplayState state) override
201     {
202         pImpl_->NotifyDisplayStateChanged(id, state);
203     }
204 private:
205     sptr<Impl> pImpl_;
206 };
207 
208 class DisplayManager::Impl::DisplayManagerScreenshotAgent : public DisplayManagerAgentDefault {
209 public:
DisplayManagerScreenshotAgent(sptr<Impl> impl)210     explicit DisplayManagerScreenshotAgent(sptr<Impl> impl) : pImpl_(impl)
211     {
212     }
213     ~DisplayManagerScreenshotAgent() = default;
214 
OnScreenshot(sptr<ScreenshotInfo> info)215     virtual void OnScreenshot(sptr<ScreenshotInfo> info) override
216     {
217         pImpl_->NotifyScreenshot(info);
218     }
219 private:
220     sptr<Impl> pImpl_;
221 };
222 
223 class DisplayManager::Impl::DisplayManagerPrivateWindowAgent : public DisplayManagerAgentDefault {
224 public:
DisplayManagerPrivateWindowAgent(sptr<Impl> impl)225     explicit DisplayManagerPrivateWindowAgent(sptr<Impl> impl) : pImpl_(impl)
226     {
227     }
228     ~DisplayManagerPrivateWindowAgent() = default;
229 
NotifyPrivateWindowStateChanged(bool hasPrivate)230     virtual void NotifyPrivateWindowStateChanged(bool hasPrivate) override
231     {
232         pImpl_->NotifyPrivateWindowStateChanged(hasPrivate);
233     }
234 private:
235     sptr<Impl> pImpl_;
236 };
237 
238 class DisplayManager::Impl::DisplayManagerFoldStatusAgent : public DisplayManagerAgentDefault {
239 public:
DisplayManagerFoldStatusAgent(sptr<Impl> impl)240     explicit DisplayManagerFoldStatusAgent(sptr<Impl> impl) : pImpl_(impl)
241     {
242     }
243     ~DisplayManagerFoldStatusAgent() = default;
244 
NotifyFoldStatusChanged(FoldStatus foldStatus)245     virtual void NotifyFoldStatusChanged(FoldStatus foldStatus) override
246     {
247         pImpl_->NotifyFoldStatusChanged(foldStatus);
248     }
249 private:
250     sptr<Impl> pImpl_;
251 };
252 
253 class DisplayManager::Impl::DisplayManagerDisplayModeAgent : public DisplayManagerAgentDefault {
254 public:
DisplayManagerDisplayModeAgent(sptr<Impl> impl)255     explicit DisplayManagerDisplayModeAgent(sptr<Impl> impl) : pImpl_(impl)
256     {
257     }
258     ~DisplayManagerDisplayModeAgent() = default;
259 
NotifyDisplayModeChanged(FoldDisplayMode displayMode)260     virtual void NotifyDisplayModeChanged(FoldDisplayMode displayMode) override
261     {
262         pImpl_->NotifyDisplayModeChanged(displayMode);
263     }
264 private:
265     sptr<Impl> pImpl_;
266 };
267 
CheckRectValid(const Media::Rect & rect,int32_t oriHeight,int32_t oriWidth) const268 bool DisplayManager::Impl::CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const
269 {
270     if (rect.left < 0) {
271         return false;
272     }
273     if (rect.top < 0) {
274         return false;
275     }
276     if (rect.width < 0) {
277         return false;
278     }
279     if (rect.height < 0) {
280         return false;
281     }
282     if (rect.width + rect.left > oriWidth) {
283         return false;
284     }
285     if (rect.height + rect.top > oriHeight) {
286         return false;
287     }
288     return true;
289 }
290 
CheckSizeValid(const Media::Size & size,int32_t oriHeight,int32_t oriWidth) const291 bool DisplayManager::Impl::CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const
292 {
293     if (size.width < 0) {
294         return false;
295     }
296     if (size.height < 0) {
297         return false;
298     }
299     if (size.width > MAX_RESOLUTION_SIZE_SCREENSHOT) {
300         return false;
301     }
302     if (size.height > MAX_RESOLUTION_SIZE_SCREENSHOT) {
303         return false;
304     }
305     return true;
306 }
307 
ClearDisplayStateCallback()308 void DisplayManager::Impl::ClearDisplayStateCallback()
309 {
310     std::lock_guard<std::recursive_mutex> lock(mutex_);
311     displayStateCallback_ = nullptr;
312     if (displayStateAgent_ != nullptr) {
313         SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(displayStateAgent_,
314             DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
315         displayStateAgent_ = nullptr;
316     }
317 }
318 
Clear()319 void DisplayManager::Impl::Clear()
320 {
321     std::lock_guard<std::recursive_mutex> lock(mutex_);
322     DMError res = DMError::DM_OK;
323     if (displayManagerListener_ != nullptr) {
324         res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
325             displayManagerListener_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
326     }
327     displayManagerListener_ = nullptr;
328     if (res != DMError::DM_OK) {
329         WLOGFW("UnregisterDisplayManagerAgent DISPLAY_EVENT_LISTENER failed !");
330     }
331     res = DMError::DM_OK;
332     if (powerEventListenerAgent_ != nullptr) {
333         res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
334             powerEventListenerAgent_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
335     }
336     powerEventListenerAgent_ = nullptr;
337     if (res != DMError::DM_OK) {
338         WLOGFW("UnregisterDisplayManagerAgent DISPLAY_POWER_EVENT_LISTENER failed !");
339     }
340     ClearDisplayStateCallback();
341 }
342 
~Impl()343 DisplayManager::Impl::~Impl()
344 {
345     Clear();
346 }
347 
DisplayManager()348 DisplayManager::DisplayManager() : pImpl_(new Impl(mutex_))
349 {
350 }
351 
~DisplayManager()352 DisplayManager::~DisplayManager()
353 {
354     std::lock_guard<std::recursive_mutex> lock(mutex_);
355     destroyed_ = true;
356 }
357 
GetDefaultDisplayId()358 DisplayId DisplayManager::GetDefaultDisplayId()
359 {
360     auto info = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
361     if (info == nullptr) {
362         return DISPLAY_ID_INVALID;
363     }
364     return info->GetDisplayId();
365 }
366 
GetDefaultDisplay()367 sptr<Display> DisplayManager::Impl::GetDefaultDisplay()
368 {
369     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
370     if (displayInfo == nullptr) {
371         return nullptr;
372     }
373     auto displayId = displayInfo->GetDisplayId();
374     std::lock_guard<std::recursive_mutex> lock(mutex_);
375     if (!UpdateDisplayInfoLocked(displayInfo)) {
376         displayMap_.erase(displayId);
377         return nullptr;
378     }
379     return displayMap_[displayId];
380 }
381 
GetDefaultDisplaySync()382 sptr<Display> DisplayManager::Impl::GetDefaultDisplaySync()
383 {
384     static std::chrono::steady_clock::time_point lastRequestTime = std::chrono::steady_clock::now();
385     auto currentTime = std::chrono::steady_clock::now();
386     auto interval = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - lastRequestTime).count();
387     if (defaultDisplayId_ != DISPLAY_ID_INVALID && interval < MAX_INTERVAL_US) {
388         std::lock_guard<std::recursive_mutex> lock(mutex_);
389         auto iter = displayMap_.find(defaultDisplayId_);
390         if (iter != displayMap_.end()) {
391             return displayMap_[defaultDisplayId_];
392         }
393     }
394 
395     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
396     if (displayInfo == nullptr) {
397         return nullptr;
398     }
399     auto displayId = displayInfo->GetDisplayId();
400     std::lock_guard<std::recursive_mutex> lock(mutex_);
401     if (!UpdateDisplayInfoLocked(displayInfo)) {
402         displayMap_.erase(displayId);
403         return nullptr;
404     }
405     lastRequestTime = currentTime;
406     defaultDisplayId_ = displayId;
407     return displayMap_[displayId];
408 }
409 
GetDisplayById(DisplayId displayId)410 sptr<Display> DisplayManager::Impl::GetDisplayById(DisplayId displayId)
411 {
412     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfo(displayId);
413     std::lock_guard<std::recursive_mutex> lock(mutex_);
414     if (!UpdateDisplayInfoLocked(displayInfo)) {
415         displayMap_.erase(displayId);
416         return nullptr;
417     }
418     return displayMap_[displayId];
419 }
420 
GetDisplayById(DisplayId displayId)421 sptr<Display> DisplayManager::GetDisplayById(DisplayId displayId)
422 {
423     std::lock_guard<std::recursive_mutex> lock(mutex_);
424     if (destroyed_) {
425         return nullptr;
426     }
427     return pImpl_->GetDisplayById(displayId);
428 }
429 
GetDisplayByScreen(ScreenId screenId)430 sptr<Display> DisplayManager::GetDisplayByScreen(ScreenId screenId)
431 {
432     if (screenId == SCREEN_ID_INVALID) {
433         WLOGFE("screenId is invalid.");
434         return nullptr;
435     }
436     sptr<Display> display = pImpl_->GetDisplayByScreenId(screenId);
437     if (display == nullptr) {
438         WLOGFE("get display by screenId failed. screen %{public}" PRIu64"", screenId);
439     }
440     return display;
441 }
442 
GetDisplayByScreenId(ScreenId screenId)443 sptr<Display> DisplayManager::Impl::GetDisplayByScreenId(ScreenId screenId)
444 {
445     sptr<DisplayInfo> displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfoByScreenId(screenId);
446     if (displayInfo == nullptr) {
447         WLOGFE("get display by screenId: displayInfo is null");
448         return nullptr;
449     }
450     DisplayId displayId = displayInfo->GetDisplayId();
451     if (displayId == DISPLAY_ID_INVALID) {
452         WLOGFE("get display by screenId: invalid displayInfo");
453         return nullptr;
454     }
455 
456     std::lock_guard<std::recursive_mutex> lock(mutex_);
457     if (!UpdateDisplayInfoLocked(displayInfo)) {
458         displayMap_.erase(displayId);
459         return nullptr;
460     }
461     return displayMap_[displayId];
462 }
463 
GetScreenshot(DisplayId displayId,DmErrorCode * errorCode)464 std::shared_ptr<Media::PixelMap> DisplayManager::GetScreenshot(DisplayId displayId, DmErrorCode* errorCode)
465 {
466     if (displayId == DISPLAY_ID_INVALID) {
467         WLOGFE("displayId invalid!");
468         return nullptr;
469     }
470     std::shared_ptr<Media::PixelMap> screenShot =
471         SingletonContainer::Get<DisplayManagerAdapter>().GetDisplaySnapshot(displayId, errorCode);
472     if (screenShot == nullptr) {
473         WLOGFE("DisplayManager::GetScreenshot failed!");
474         return nullptr;
475     }
476 
477     return screenShot;
478 }
479 
GetScreenshot(DisplayId displayId,const Media::Rect & rect,const Media::Size & size,int rotation,DmErrorCode * errorCode)480 std::shared_ptr<Media::PixelMap> DisplayManager::GetScreenshot(DisplayId displayId, const Media::Rect &rect,
481     const Media::Size &size, int rotation, DmErrorCode* errorCode)
482 {
483     if (displayId == DISPLAY_ID_INVALID) {
484         WLOGFE("displayId invalid!");
485         return nullptr;
486     }
487 
488     std::shared_ptr<Media::PixelMap> screenShot =
489         SingletonContainer::Get<DisplayManagerAdapter>().GetDisplaySnapshot(displayId, errorCode);
490     if (screenShot == nullptr) {
491         WLOGFE("DisplayManager::GetScreenshot failed!");
492         return nullptr;
493     }
494 
495     // check parameters
496     int32_t oriHeight = screenShot->GetHeight();
497     int32_t oriWidth = screenShot->GetWidth();
498     if (!pImpl_->CheckRectValid(rect, oriHeight, oriWidth)) {
499         WLOGFE("rect invalid! left %{public}d, top %{public}d, w %{public}d, h %{public}d",
500             rect.left, rect.top, rect.width, rect.height);
501         return nullptr;
502     }
503     if (!pImpl_->CheckSizeValid(size, oriHeight, oriWidth)) {
504         WLOGFE("size invalid! w %{public}d, h %{public}d", rect.width, rect.height);
505         return nullptr;
506     }
507 
508     // create crop dest pixelmap
509     Media::InitializationOptions opt;
510     opt.size.width = size.width;
511     opt.size.height = size.height;
512     opt.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
513     opt.editable = false;
514     auto pixelMap = Media::PixelMap::Create(*screenShot, rect, opt);
515     if (pixelMap == nullptr) {
516         WLOGFE("Media::PixelMap::Create failed!");
517         return nullptr;
518     }
519     std::shared_ptr<Media::PixelMap> dstScreenshot(pixelMap.release());
520 
521     return dstScreenshot;
522 }
523 
GetDefaultDisplay()524 sptr<Display> DisplayManager::GetDefaultDisplay()
525 {
526     return pImpl_->GetDefaultDisplay();
527 }
528 
GetDefaultDisplaySync()529 sptr<Display> DisplayManager::GetDefaultDisplaySync()
530 {
531     return pImpl_->GetDefaultDisplaySync();
532 }
533 
GetAllDisplayIds()534 std::vector<DisplayId> DisplayManager::GetAllDisplayIds()
535 {
536     return SingletonContainer::Get<DisplayManagerAdapter>().GetAllDisplayIds();
537 }
538 
GetAllDisplays()539 std::vector<sptr<Display>> DisplayManager::GetAllDisplays()
540 {
541     std::vector<sptr<Display>> res;
542     auto displayIds = GetAllDisplayIds();
543     for (auto displayId: displayIds) {
544         const sptr<Display> display = GetDisplayById(displayId);
545         if (display != nullptr) {
546             res.emplace_back(display);
547         } else {
548             WLOGFE("DisplayManager::GetAllDisplays display %" PRIu64" nullptr!", displayId);
549         }
550     }
551     return res;
552 }
553 
HasPrivateWindow(DisplayId displayId,bool & hasPrivateWindow)554 DMError DisplayManager::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
555 {
556     return pImpl_->HasPrivateWindow(displayId, hasPrivateWindow);
557 }
558 
HasPrivateWindow(DisplayId displayId,bool & hasPrivateWindow)559 DMError DisplayManager::Impl::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
560 {
561     return SingletonContainer::Get<DisplayManagerAdapter>().HasPrivateWindow(displayId, hasPrivateWindow);
562 }
563 
IsFoldable()564 bool DisplayManager::IsFoldable()
565 {
566     return pImpl_->IsFoldable();
567 }
568 
IsFoldable()569 bool DisplayManager::Impl::IsFoldable()
570 {
571     return SingletonContainer::Get<DisplayManagerAdapter>().IsFoldable();
572 }
573 
GetFoldStatus()574 FoldStatus DisplayManager::GetFoldStatus()
575 {
576     return pImpl_->GetFoldStatus();
577 }
578 
GetFoldStatus()579 FoldStatus DisplayManager::Impl::GetFoldStatus()
580 {
581     return SingletonContainer::Get<DisplayManagerAdapter>().GetFoldStatus();
582 }
583 
GetFoldDisplayMode()584 FoldDisplayMode DisplayManager::GetFoldDisplayMode()
585 {
586     return pImpl_->GetFoldDisplayMode();
587 }
588 
GetFoldDisplayMode()589 FoldDisplayMode DisplayManager::Impl::GetFoldDisplayMode()
590 {
591     return SingletonContainer::Get<DisplayManagerAdapter>().GetFoldDisplayMode();
592 }
593 
SetFoldDisplayMode(const FoldDisplayMode mode)594 void DisplayManager::SetFoldDisplayMode(const FoldDisplayMode mode)
595 {
596     pImpl_->SetFoldDisplayMode(mode);
597 }
598 
SetFoldDisplayMode(const FoldDisplayMode mode)599 void DisplayManager::Impl::SetFoldDisplayMode(const FoldDisplayMode mode)
600 {
601     SingletonContainer::Get<DisplayManagerAdapter>().SetFoldDisplayMode(mode);
602 }
603 
GetCurrentFoldCreaseRegion()604 sptr<FoldCreaseRegion> DisplayManager::GetCurrentFoldCreaseRegion()
605 {
606     return pImpl_->GetCurrentFoldCreaseRegion();
607 }
608 
GetCurrentFoldCreaseRegion()609 sptr<FoldCreaseRegion> DisplayManager::Impl::GetCurrentFoldCreaseRegion()
610 {
611     return SingletonContainer::Get<DisplayManagerAdapter>().GetCurrentFoldCreaseRegion();
612 }
613 
RegisterDisplayListener(sptr<IDisplayListener> listener)614 DMError DisplayManager::Impl::RegisterDisplayListener(sptr<IDisplayListener> listener)
615 {
616     std::lock_guard<std::recursive_mutex> lock(mutex_);
617     DMError ret = DMError::DM_OK;
618     if (displayManagerListener_ == nullptr) {
619         displayManagerListener_ = new DisplayManagerListener(this);
620         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
621             displayManagerListener_,
622             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
623     }
624     if (ret != DMError::DM_OK) {
625         WLOGFW("RegisterDisplayManagerAgent failed !");
626         displayManagerListener_ = nullptr;
627     } else {
628         displayListeners_.insert(listener);
629     }
630     return ret;
631 }
632 
NotifyPrivateWindowStateChanged(bool hasPrivate)633 void DisplayManager::Impl::NotifyPrivateWindowStateChanged(bool hasPrivate)
634 {
635     std::set<sptr<IPrivateWindowListener>> privateWindowListeners;
636     {
637         std::lock_guard<std::recursive_mutex> lock(mutex_);
638         privateWindowListeners = privateWindowListeners_;
639     }
640     for (auto& listener : privateWindowListeners) {
641         listener->OnPrivateWindow(hasPrivate);
642     }
643 }
644 
RegisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)645 DMError DisplayManager::RegisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)
646 {
647     if (listener == nullptr) {
648         WLOGFE("RegisterPrivateWindowListener listener is nullptr.");
649         return DMError::DM_ERROR_NULLPTR;
650     }
651     return pImpl_->RegisterPrivateWindowListener(listener);
652 }
653 
UnregisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)654 DMError DisplayManager::UnregisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)
655 {
656     if (listener == nullptr) {
657         WLOGFE("UnregisterPrivateWindowListener listener is nullptr.");
658         return DMError::DM_ERROR_NULLPTR;
659     }
660     return pImpl_->UnregisterPrivateWindowListener(listener);
661 }
662 
RegisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)663 DMError DisplayManager::Impl::RegisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)
664 {
665     std::lock_guard<std::recursive_mutex> lock(mutex_);
666     DMError ret = DMError::DM_OK;
667     if (privateWindowListenerAgent_ == nullptr) {
668         privateWindowListenerAgent_ = new DisplayManagerPrivateWindowAgent(this);
669         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
670             privateWindowListenerAgent_,
671             DisplayManagerAgentType::PRIVATE_WINDOW_LISTENER);
672     }
673     if (ret != DMError::DM_OK) {
674         WLOGFW("RegisterDisplayManagerAgent failed !");
675         privateWindowListenerAgent_ = nullptr;
676     } else {
677         WLOGI("privateWindowListener register success");
678         privateWindowListeners_.insert(listener);
679     }
680     return ret;
681 }
682 
UnregisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)683 DMError DisplayManager::Impl::UnregisterPrivateWindowListener(sptr<IPrivateWindowListener> listener)
684 {
685     std::lock_guard<std::recursive_mutex> lock(mutex_);
686     auto iter = std::find(privateWindowListeners_.begin(), privateWindowListeners_.end(), listener);
687     if (iter == privateWindowListeners_.end()) {
688         WLOGFE("could not find this listener");
689         return DMError::DM_ERROR_NULLPTR;
690     }
691     privateWindowListeners_.erase(iter);
692     DMError ret = DMError::DM_OK;
693     if (privateWindowListeners_.empty() && privateWindowListenerAgent_ != nullptr) {
694         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
695             privateWindowListenerAgent_,
696             DisplayManagerAgentType::PRIVATE_WINDOW_LISTENER);
697         privateWindowListenerAgent_ = nullptr;
698     }
699     return ret;
700 }
RegisterDisplayListener(sptr<IDisplayListener> listener)701 DMError DisplayManager::RegisterDisplayListener(sptr<IDisplayListener> listener)
702 {
703     if (listener == nullptr) {
704         WLOGFE("RegisterDisplayListener listener is nullptr.");
705         return DMError::DM_ERROR_NULLPTR;
706     }
707     return pImpl_->RegisterDisplayListener(listener);
708 }
709 
UnregisterDisplayListener(sptr<IDisplayListener> listener)710 DMError DisplayManager::Impl::UnregisterDisplayListener(sptr<IDisplayListener> listener)
711 {
712     std::lock_guard<std::recursive_mutex> lock(mutex_);
713     auto iter = std::find(displayListeners_.begin(), displayListeners_.end(), listener);
714     if (iter == displayListeners_.end()) {
715         WLOGFE("could not find this listener");
716         return DMError::DM_ERROR_NULLPTR;
717     }
718     displayListeners_.erase(iter);
719     DMError ret = DMError::DM_OK;
720     if (displayListeners_.empty() && displayManagerListener_ != nullptr) {
721         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
722             displayManagerListener_,
723             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
724         displayManagerListener_ = nullptr;
725     }
726     return ret;
727 }
728 
UnregisterDisplayListener(sptr<IDisplayListener> listener)729 DMError DisplayManager::UnregisterDisplayListener(sptr<IDisplayListener> listener)
730 {
731     if (listener == nullptr) {
732         WLOGFE("UnregisterDisplayListener listener is nullptr.");
733         return DMError::DM_ERROR_NULLPTR;
734     }
735     return pImpl_->UnregisterDisplayListener(listener);
736 }
737 
RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)738 DMError DisplayManager::Impl::RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
739 {
740     std::lock_guard<std::recursive_mutex> lock(mutex_);
741     DMError ret = DMError::DM_OK;
742     if (powerEventListenerAgent_ == nullptr) {
743         powerEventListenerAgent_ = new DisplayManagerAgent(this);
744         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
745             powerEventListenerAgent_,
746             DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
747     }
748     if (ret != DMError::DM_OK) {
749         WLOGFW("RegisterDisplayManagerAgent failed !");
750         powerEventListenerAgent_ = nullptr;
751     } else {
752         powerEventListeners_.insert(listener);
753     }
754     WLOGFD("RegisterDisplayPowerEventListener end");
755     return ret;
756 }
757 
RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)758 DMError DisplayManager::RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
759 {
760     if (listener == nullptr) {
761         WLOGFE("listener is nullptr");
762         return DMError::DM_ERROR_NULLPTR;
763     }
764     return pImpl_->RegisterDisplayPowerEventListener(listener);
765 }
766 
UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)767 DMError DisplayManager::Impl::UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
768 {
769     std::lock_guard<std::recursive_mutex> lock(mutex_);
770     auto iter = std::find(powerEventListeners_.begin(), powerEventListeners_.end(), listener);
771     if (iter == powerEventListeners_.end()) {
772         WLOGFE("could not find this listener");
773         return DMError::DM_ERROR_NULLPTR;
774     }
775     powerEventListeners_.erase(iter);
776     DMError ret = DMError::DM_OK;
777     if (powerEventListeners_.empty() && powerEventListenerAgent_ != nullptr) {
778         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
779             powerEventListenerAgent_,
780             DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
781         powerEventListenerAgent_ = nullptr;
782     }
783     WLOGFD("UnregisterDisplayPowerEventListener end");
784     return ret;
785 }
786 
UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)787 DMError DisplayManager::UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
788 {
789     if (listener == nullptr) {
790         WLOGFE("listener is nullptr");
791         return DMError::DM_ERROR_NULLPTR;
792     }
793     return pImpl_->UnregisterDisplayPowerEventListener(listener);
794 }
795 
RegisterScreenshotListener(sptr<IScreenshotListener> listener)796 DMError DisplayManager::Impl::RegisterScreenshotListener(sptr<IScreenshotListener> listener)
797 {
798     std::lock_guard<std::recursive_mutex> lock(mutex_);
799     DMError ret = DMError::DM_OK;
800     if (screenshotListenerAgent_ == nullptr) {
801         screenshotListenerAgent_ = new DisplayManagerScreenshotAgent(this);
802         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
803             screenshotListenerAgent_,
804             DisplayManagerAgentType::SCREENSHOT_EVENT_LISTENER);
805     }
806     if (ret != DMError::DM_OK) {
807         WLOGFW("RegisterDisplayManagerAgent failed !");
808         screenshotListenerAgent_ = nullptr;
809     } else {
810         screenshotListeners_.insert(listener);
811     }
812     return ret;
813 }
814 
RegisterScreenshotListener(sptr<IScreenshotListener> listener)815 DMError DisplayManager::RegisterScreenshotListener(sptr<IScreenshotListener> listener)
816 {
817     if (listener == nullptr) {
818         WLOGFE("RegisterScreenshotListener listener is nullptr.");
819         return DMError::DM_ERROR_NULLPTR;
820     }
821     return pImpl_->RegisterScreenshotListener(listener);
822 }
823 
UnregisterScreenshotListener(sptr<IScreenshotListener> listener)824 DMError DisplayManager::Impl::UnregisterScreenshotListener(sptr<IScreenshotListener> listener)
825 {
826     std::lock_guard<std::recursive_mutex> lock(mutex_);
827     auto iter = std::find(screenshotListeners_.begin(), screenshotListeners_.end(), listener);
828     if (iter == screenshotListeners_.end()) {
829         WLOGFE("could not find this listener");
830         return DMError::DM_ERROR_NULLPTR;
831     }
832     screenshotListeners_.erase(iter);
833     DMError ret = DMError::DM_OK;
834     if (screenshotListeners_.empty() && screenshotListenerAgent_ != nullptr) {
835         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
836             screenshotListenerAgent_,
837             DisplayManagerAgentType::SCREENSHOT_EVENT_LISTENER);
838         screenshotListenerAgent_ = nullptr;
839     }
840     return ret;
841 }
842 
UnregisterScreenshotListener(sptr<IScreenshotListener> listener)843 DMError DisplayManager::UnregisterScreenshotListener(sptr<IScreenshotListener> listener)
844 {
845     if (listener == nullptr) {
846         WLOGFE("UnregisterScreenshotListener listener is nullptr.");
847         return DMError::DM_ERROR_NULLPTR;
848     }
849     return pImpl_->UnregisterScreenshotListener(listener);
850 }
851 
NotifyFoldStatusChanged(FoldStatus foldStatus)852 void DisplayManager::Impl::NotifyFoldStatusChanged(FoldStatus foldStatus)
853 {
854     std::set<sptr<IFoldStatusListener>> foldStatusListeners;
855     {
856         std::lock_guard<std::recursive_mutex> lock(mutex_);
857         foldStatusListeners = foldStatusListeners_;
858     }
859     for (auto& listener : foldStatusListeners) {
860         listener->OnFoldStatusChanged(foldStatus);
861     }
862 }
863 
RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)864 DMError DisplayManager::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
865 {
866     if (listener == nullptr) {
867         WLOGFE("IFoldStatusListener listener is nullptr.");
868         return DMError::DM_ERROR_NULLPTR;
869     }
870     return pImpl_->RegisterFoldStatusListener(listener);
871 }
872 
RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)873 DMError DisplayManager::Impl::RegisterFoldStatusListener(sptr<IFoldStatusListener> listener)
874 {
875     std::lock_guard<std::recursive_mutex> lock(mutex_);
876     DMError ret = DMError::DM_OK;
877     if (foldStatusListenerAgent_ == nullptr) {
878         foldStatusListenerAgent_ = new DisplayManagerFoldStatusAgent(this);
879         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
880             foldStatusListenerAgent_,
881             DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
882     }
883     if (ret != DMError::DM_OK) {
884         WLOGFW("RegisterFoldStatusListener failed !");
885         foldStatusListenerAgent_ = nullptr;
886     } else {
887         WLOGI("IFoldStatusListener register success");
888         foldStatusListeners_.insert(listener);
889     }
890     return ret;
891 }
892 
UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)893 DMError DisplayManager::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
894 {
895     if (listener == nullptr) {
896         WLOGFE("UnregisterFoldStatusListener listener is nullptr.");
897         return DMError::DM_ERROR_NULLPTR;
898     }
899     return pImpl_->UnregisterFoldStatusListener(listener);
900 }
901 
UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)902 DMError DisplayManager::Impl::UnregisterFoldStatusListener(sptr<IFoldStatusListener> listener)
903 {
904     std::lock_guard<std::recursive_mutex> lock(mutex_);
905     auto iter = std::find(foldStatusListeners_.begin(), foldStatusListeners_.end(), listener);
906     if (iter == foldStatusListeners_.end()) {
907         WLOGFE("could not find this listener");
908         return DMError::DM_ERROR_NULLPTR;
909     }
910     foldStatusListeners_.erase(iter);
911     DMError ret = DMError::DM_OK;
912     if (foldStatusListeners_.empty() && foldStatusListenerAgent_ != nullptr) {
913         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
914             foldStatusListenerAgent_,
915             DisplayManagerAgentType::FOLD_STATUS_CHANGED_LISTENER);
916         foldStatusListenerAgent_ = nullptr;
917     }
918     return ret;
919 }
920 
NotifyDisplayModeChanged(FoldDisplayMode displayMode)921 void DisplayManager::Impl::NotifyDisplayModeChanged(FoldDisplayMode displayMode)
922 {
923     std::set<sptr<IDisplayModeListener>> displayModeListeners;
924     {
925         std::lock_guard<std::recursive_mutex> lock(mutex_);
926         displayModeListeners = displayModeListeners_;
927     }
928     for (auto& listener : displayModeListeners) {
929         listener->OnDisplayModeChanged(displayMode);
930     }
931 }
932 
RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)933 DMError DisplayManager::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
934 {
935     if (listener == nullptr) {
936         WLOGFE("IDisplayModeListener listener is nullptr.");
937         return DMError::DM_ERROR_NULLPTR;
938     }
939     return pImpl_->RegisterDisplayModeListener(listener);
940 }
941 
RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)942 DMError DisplayManager::Impl::RegisterDisplayModeListener(sptr<IDisplayModeListener> listener)
943 {
944     std::lock_guard<std::recursive_mutex> lock(mutex_);
945     DMError ret = DMError::DM_OK;
946     if (displayModeListenerAgent_ == nullptr) {
947         displayModeListenerAgent_ = new DisplayManagerDisplayModeAgent(this);
948         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
949             displayModeListenerAgent_,
950             DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
951     }
952     if (ret != DMError::DM_OK) {
953         WLOGFW("RegisterDisplayModeListener failed !");
954         displayModeListenerAgent_ = nullptr;
955     } else {
956         WLOGI("IDisplayModeListener register success");
957         displayModeListeners_.insert(listener);
958     }
959     return ret;
960 }
961 
UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)962 DMError DisplayManager::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
963 {
964     if (listener == nullptr) {
965         WLOGFE("UnregisterPrivateWindowListener listener is nullptr.");
966         return DMError::DM_ERROR_NULLPTR;
967     }
968     return pImpl_->UnregisterDisplayModeListener(listener);
969 }
970 
UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)971 DMError DisplayManager::Impl::UnregisterDisplayModeListener(sptr<IDisplayModeListener> listener)
972 {
973     std::lock_guard<std::recursive_mutex> lock(mutex_);
974     auto iter = std::find(displayModeListeners_.begin(), displayModeListeners_.end(), listener);
975     if (iter == displayModeListeners_.end()) {
976         WLOGFE("could not find this listener");
977         return DMError::DM_ERROR_NULLPTR;
978     }
979     displayModeListeners_.erase(iter);
980     DMError ret = DMError::DM_OK;
981     if (displayModeListeners_.empty() && displayModeListenerAgent_ != nullptr) {
982         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
983             displayModeListenerAgent_,
984             DisplayManagerAgentType::DISPLAY_MODE_CHANGED_LISTENER);
985         displayModeListenerAgent_ = nullptr;
986     }
987     return ret;
988 }
989 
NotifyScreenshot(sptr<ScreenshotInfo> info)990 void DisplayManager::Impl::NotifyScreenshot(sptr<ScreenshotInfo> info)
991 {
992     WLOGFD("NotifyScreenshot trigger:[%{public}s] displayId:%{public}" PRIu64" size:%{public}zu",
993         info->GetTrigger().c_str(), info->GetDisplayId(), screenshotListeners_.size());
994     std::set<sptr<IScreenshotListener>> screenshotListeners;
995     {
996         std::lock_guard<std::recursive_mutex> lock(mutex_);
997         screenshotListeners = screenshotListeners_;
998     }
999     for (auto& listener : screenshotListeners) {
1000         listener->OnScreenshot(*info);
1001     }
1002 }
1003 
NotifyDisplayPowerEvent(DisplayPowerEvent event,EventStatus status)1004 void DisplayManager::Impl::NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status)
1005 {
1006     WLOGFD("NotifyDisplayPowerEvent event:%{public}u, status:%{public}u, size:%{public}zu", event, status,
1007         powerEventListeners_.size());
1008     std::set<sptr<IDisplayPowerEventListener>> powerEventListeners;
1009     {
1010         std::lock_guard<std::recursive_mutex> lock(mutex_);
1011         powerEventListeners = powerEventListeners_;
1012     }
1013     for (auto& listener : powerEventListeners) {
1014         listener->OnDisplayPowerEvent(event, status);
1015     }
1016 }
1017 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)1018 void DisplayManager::Impl::NotifyDisplayStateChanged(DisplayId id, DisplayState state)
1019 {
1020     WLOGFD("state:%{public}u", state);
1021     std::lock_guard<std::recursive_mutex> lock(mutex_);
1022     DisplayStateCallback displayStateCallback = displayStateCallback_;
1023     if (displayStateCallback) {
1024         displayStateCallback(state);
1025         ClearDisplayStateCallback();
1026         return;
1027     }
1028     WLOGFW("callback_ target is not set!");
1029 }
1030 
NotifyDisplayCreate(sptr<DisplayInfo> info)1031 void DisplayManager::Impl::NotifyDisplayCreate(sptr<DisplayInfo> info)
1032 {
1033     std::lock_guard<std::recursive_mutex> lock(mutex_);
1034     UpdateDisplayInfoLocked(info);
1035 }
1036 
NotifyDisplayDestroy(DisplayId displayId)1037 void DisplayManager::Impl::NotifyDisplayDestroy(DisplayId displayId)
1038 {
1039     WLOGFD("displayId:%{public}" PRIu64".", displayId);
1040     std::lock_guard<std::recursive_mutex> lock(mutex_);
1041     displayMap_.erase(displayId);
1042 }
1043 
NotifyDisplayChange(sptr<DisplayInfo> displayInfo)1044 void DisplayManager::Impl::NotifyDisplayChange(sptr<DisplayInfo> displayInfo)
1045 {
1046     std::lock_guard<std::recursive_mutex> lock(mutex_);
1047     UpdateDisplayInfoLocked(displayInfo);
1048 }
1049 
UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)1050 bool DisplayManager::Impl::UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)
1051 {
1052     if (displayInfo == nullptr) {
1053         WLOGFE("displayInfo is null");
1054         return false;
1055     }
1056     DisplayId displayId = displayInfo->GetDisplayId();
1057     WLOGFD("displayId:%{public}" PRIu64".", displayId);
1058     if (displayId == DISPLAY_ID_INVALID) {
1059         WLOGFE("displayId is invalid.");
1060         return false;
1061     }
1062     auto iter = displayMap_.find(displayId);
1063     if (iter != displayMap_.end() && iter->second != nullptr) {
1064         WLOGFD("get screen in screen map");
1065         iter->second->UpdateDisplayInfo(displayInfo);
1066         return true;
1067     }
1068     sptr<Display> display = new Display("", displayInfo);
1069     displayMap_[displayId] = display;
1070     return true;
1071 }
1072 
WakeUpBegin(PowerStateChangeReason reason)1073 bool DisplayManager::WakeUpBegin(PowerStateChangeReason reason)
1074 {
1075     WLOGFD("WakeUpBegin start, reason:%{public}u", reason);
1076     return SingletonContainer::Get<DisplayManagerAdapter>().WakeUpBegin(reason);
1077 }
1078 
WakeUpEnd()1079 bool DisplayManager::WakeUpEnd()
1080 {
1081     WLOGFD("WakeUpEnd start");
1082     return SingletonContainer::Get<DisplayManagerAdapter>().WakeUpEnd();
1083 }
1084 
SuspendBegin(PowerStateChangeReason reason)1085 bool DisplayManager::SuspendBegin(PowerStateChangeReason reason)
1086 {
1087     // dms->wms notify other windows to hide
1088     WLOGFD("SuspendBegin start, reason:%{public}u", reason);
1089     return SingletonContainer::Get<DisplayManagerAdapter>().SuspendBegin(reason);
1090 }
1091 
SuspendEnd()1092 bool DisplayManager::SuspendEnd()
1093 {
1094     WLOGFD("SuspendEnd start");
1095     return SingletonContainer::Get<DisplayManagerAdapter>().SuspendEnd();
1096 }
1097 
SetDisplayState(DisplayState state,DisplayStateCallback callback)1098 bool DisplayManager::Impl::SetDisplayState(DisplayState state, DisplayStateCallback callback)
1099 {
1100     WLOGFD("state:%{public}u", state);
1101     bool ret = true;
1102     {
1103         std::lock_guard<std::recursive_mutex> lock(mutex_);
1104         if (displayStateCallback_ != nullptr || callback == nullptr) {
1105             WLOGFI("previous callback not called or callback invalid");
1106             return false;
1107         }
1108         displayStateCallback_ = callback;
1109 
1110         if (displayStateAgent_ == nullptr) {
1111             displayStateAgent_ = new DisplayManagerAgent(this);
1112             ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
1113                 displayStateAgent_,
1114                 DisplayManagerAgentType::DISPLAY_STATE_LISTENER) == DMError::DM_OK;
1115         }
1116     }
1117     ret = ret && SingletonContainer::Get<DisplayManagerAdapter>().SetDisplayState(state);
1118     if (!ret) {
1119         ClearDisplayStateCallback();
1120     }
1121     return ret;
1122 }
1123 
SetDisplayState(DisplayState state,DisplayStateCallback callback)1124 bool DisplayManager::SetDisplayState(DisplayState state, DisplayStateCallback callback)
1125 {
1126     return pImpl_->SetDisplayState(state, callback);
1127 }
1128 
GetDisplayState(DisplayId displayId)1129 DisplayState DisplayManager::GetDisplayState(DisplayId displayId)
1130 {
1131     return SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayState(displayId);
1132 }
1133 
SetScreenBrightness(uint64_t screenId,uint32_t level)1134 bool DisplayManager::SetScreenBrightness(uint64_t screenId, uint32_t level)
1135 {
1136     WLOGFD("screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
1137     RSInterfaces::GetInstance().SetScreenBacklight(screenId, level);
1138     return true;
1139 }
1140 
GetScreenBrightness(uint64_t screenId) const1141 uint32_t DisplayManager::GetScreenBrightness(uint64_t screenId) const
1142 {
1143     uint32_t level = static_cast<uint32_t>(RSInterfaces::GetInstance().GetScreenBacklight(screenId));
1144     WLOGFD("screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
1145     return level;
1146 }
1147 
NotifyDisplayEvent(DisplayEvent event)1148 void DisplayManager::NotifyDisplayEvent(DisplayEvent event)
1149 {
1150     // Unlock event dms->wms restore other hidden windows
1151     WLOGFD("DisplayEvent:%{public}u", event);
1152     SingletonContainer::Get<DisplayManagerAdapter>().NotifyDisplayEvent(event);
1153 }
1154 
Freeze(std::vector<DisplayId> displayIds)1155 bool DisplayManager::Freeze(std::vector<DisplayId> displayIds)
1156 {
1157     WLOGFD("freeze display");
1158     if (displayIds.size() == 0) {
1159         WLOGFE("freeze display fail, num of display is 0");
1160         return false;
1161     }
1162     if (displayIds.size() > MAX_DISPLAY_SIZE) {
1163         WLOGFE("freeze display fail, displayIds size is bigger than %{public}u.", MAX_DISPLAY_SIZE);
1164         return false;
1165     }
1166     return SingletonContainer::Get<DisplayManagerAdapter>().SetFreeze(displayIds, true);
1167 }
1168 
Unfreeze(std::vector<DisplayId> displayIds)1169 bool DisplayManager::Unfreeze(std::vector<DisplayId> displayIds)
1170 {
1171     WLOGFD("unfreeze display");
1172     if (displayIds.size() == 0) {
1173         WLOGFE("unfreeze display fail, num of display is 0");
1174         return false;
1175     }
1176     if (displayIds.size() > MAX_DISPLAY_SIZE) {
1177         WLOGFE("unfreeze display fail, displayIds size is bigger than %{public}u.", MAX_DISPLAY_SIZE);
1178         return false;
1179     }
1180     return SingletonContainer::Get<DisplayManagerAdapter>().SetFreeze(displayIds, false);
1181 }
1182 
AddSurfaceNodeToDisplay(DisplayId displayId,std::shared_ptr<class RSSurfaceNode> & surfaceNode)1183 DMError DisplayManager::AddSurfaceNodeToDisplay(DisplayId displayId,
1184     std::shared_ptr<class RSSurfaceNode>& surfaceNode)
1185 {
1186     return SingletonContainer::Get<DisplayManagerAdapter>().AddSurfaceNodeToDisplay(displayId, surfaceNode);
1187 }
1188 
RemoveSurfaceNodeFromDisplay(DisplayId displayId,std::shared_ptr<class RSSurfaceNode> & surfaceNode)1189 DMError DisplayManager::RemoveSurfaceNodeFromDisplay(DisplayId displayId,
1190     std::shared_ptr<class RSSurfaceNode>& surfaceNode)
1191 {
1192     return SingletonContainer::Get<DisplayManagerAdapter>().RemoveSurfaceNodeFromDisplay(displayId, surfaceNode);
1193 }
1194 
OnRemoteDied()1195 void DisplayManager::Impl::OnRemoteDied()
1196 {
1197     WLOGFI("dms is died");
1198     std::lock_guard<std::recursive_mutex> lock(mutex_);
1199     displayManagerListener_ = nullptr;
1200     displayStateAgent_ = nullptr;
1201     powerEventListenerAgent_ = nullptr;
1202     screenshotListenerAgent_ = nullptr;
1203     privateWindowListenerAgent_ = nullptr;
1204 }
1205 
OnRemoteDied()1206 void DisplayManager::OnRemoteDied()
1207 {
1208     pImpl_->OnRemoteDied();
1209 }
1210 } // namespace OHOS::Rosen