• 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 
22 #include "display_manager_adapter.h"
23 #include "display_manager_agent_default.h"
24 #include "dm_common.h"
25 #include "screen_manager.h"
26 #include "singleton_delegator.h"
27 #include "window_manager_hilog.h"
28 
29 namespace OHOS::Rosen {
30 namespace {
31     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManager"};
32     const static uint32_t MAX_DISPLAY_SIZE = 32;
33     const static uint32_t MAX_INTERVAL_US = 5000;
34 }
35 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManager)
36 
37 class DisplayManager::Impl : public RefBase {
38 public:
Impl(std::recursive_mutex & mutex)39     Impl(std::recursive_mutex& mutex) : mutex_(mutex) {}
40     ~Impl();
41     static inline SingletonDelegator<DisplayManager> delegator;
42     bool CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const;
43     bool CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const;
44     sptr<Display> GetDefaultDisplay();
45     sptr<Display> GetDefaultDisplaySync();
46     sptr<Display> GetDisplayById(DisplayId displayId);
47     DMError HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow);
48     bool RegisterDisplayListener(sptr<IDisplayListener> listener);
49     bool UnregisterDisplayListener(sptr<IDisplayListener> listener);
50     bool SetDisplayState(DisplayState state, DisplayStateCallback callback);
51     bool RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener);
52     bool UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener);
53     bool RegisterScreenshotListener(sptr<IScreenshotListener> listener);
54     bool UnregisterScreenshotListener(sptr<IScreenshotListener> listener);
55     sptr<Display> GetDisplayByScreenId(ScreenId screenId);
56     void OnRemoteDied();
57 private:
58     void ClearDisplayStateCallback();
59     void NotifyScreenshot(sptr<ScreenshotInfo> info);
60     void NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status);
61     void NotifyDisplayStateChanged(DisplayId id, DisplayState state);
62     void NotifyDisplayChangedEvent(sptr<DisplayInfo> info, DisplayChangeEvent event);
63     void NotifyDisplayCreate(sptr<DisplayInfo> info);
64     void NotifyDisplayDestroy(DisplayId);
65     void NotifyDisplayChange(sptr<DisplayInfo> displayInfo);
66     bool UpdateDisplayInfoLocked(sptr<DisplayInfo>);
67     void Clear();
68 
69     DisplayId defaultDisplayId_ = DISPLAY_ID_INVALID;
70     std::map<DisplayId, sptr<Display>> displayMap_;
71     DisplayStateCallback displayStateCallback_;
72     std::recursive_mutex& mutex_;
73     std::set<sptr<IDisplayListener>> displayListeners_;
74     std::set<sptr<IDisplayPowerEventListener>> powerEventListeners_;
75     std::set<sptr<IScreenshotListener>> screenshotListeners_;
76     class DisplayManagerListener;
77     sptr<DisplayManagerListener> displayManagerListener_;
78     class DisplayManagerAgent;
79     sptr<DisplayManagerAgent> displayStateAgent_;
80     sptr<DisplayManagerAgent> powerEventListenerAgent_;
81     class DisplayManagerScreenshotAgent;
82     sptr<DisplayManagerScreenshotAgent> screenshotListenerAgent_;
83 };
84 
85 class DisplayManager::Impl::DisplayManagerListener : public DisplayManagerAgentDefault {
86 public:
DisplayManagerListener(sptr<Impl> impl)87     explicit DisplayManagerListener(sptr<Impl> impl) : pImpl_(impl)
88     {
89     }
90 
OnDisplayCreate(sptr<DisplayInfo> displayInfo)91     void OnDisplayCreate(sptr<DisplayInfo> displayInfo) override
92     {
93         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
94             WLOGFE("OnDisplayCreate, displayInfo is invalid.");
95             return;
96         }
97         if (pImpl_ == nullptr) {
98             WLOGFE("OnDisplayCreate, impl is nullptr.");
99             return;
100         }
101         pImpl_->NotifyDisplayCreate(displayInfo);
102         std::set<sptr<IDisplayListener>> displayListeners;
103         {
104             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
105             displayListeners = pImpl_->displayListeners_;
106         }
107         for (auto listener : displayListeners) {
108             listener->OnCreate(displayInfo->GetDisplayId());
109         }
110     };
111 
OnDisplayDestroy(DisplayId displayId)112     void OnDisplayDestroy(DisplayId displayId) override
113     {
114         if (displayId == DISPLAY_ID_INVALID) {
115             WLOGFE("OnDisplayDestroy, displayId is invalid.");
116             return;
117         }
118         if (pImpl_ == nullptr) {
119             WLOGFE("OnDisplayDestroy, impl is nullptr.");
120             return;
121         }
122         pImpl_->NotifyDisplayDestroy(displayId);
123         std::set<sptr<IDisplayListener>> displayListeners;
124         {
125             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
126             displayListeners = pImpl_->displayListeners_;
127         }
128         for (auto listener : displayListeners) {
129             listener->OnDestroy(displayId);
130         }
131     };
132 
OnDisplayChange(sptr<DisplayInfo> displayInfo,DisplayChangeEvent event)133     void OnDisplayChange(sptr<DisplayInfo> displayInfo, DisplayChangeEvent event) override
134     {
135         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
136             WLOGFE("OnDisplayChange, displayInfo is invalid.");
137             return;
138         }
139         if (pImpl_ == nullptr) {
140             WLOGFE("OnDisplayChange, impl is nullptr.");
141             return;
142         }
143         WLOGD("OnDisplayChange. display %{public}" PRIu64", event %{public}u", displayInfo->GetDisplayId(), event);
144         pImpl_->NotifyDisplayChange(displayInfo);
145         std::set<sptr<IDisplayListener>> displayListeners;
146         {
147             std::lock_guard<std::recursive_mutex> lock(pImpl_->mutex_);
148             displayListeners = pImpl_->displayListeners_;
149         }
150         for (auto listener : displayListeners) {
151             listener->OnChange(displayInfo->GetDisplayId());
152         }
153     };
154 private:
155     sptr<Impl> pImpl_;
156 };
157 
158 class DisplayManager::Impl::DisplayManagerAgent : public DisplayManagerAgentDefault {
159 public:
DisplayManagerAgent(sptr<Impl> impl)160     explicit DisplayManagerAgent(sptr<Impl> impl) : pImpl_(impl)
161     {
162     }
163     ~DisplayManagerAgent() = default;
164 
NotifyDisplayPowerEvent(DisplayPowerEvent event,EventStatus status)165     virtual void NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) override
166     {
167         pImpl_->NotifyDisplayPowerEvent(event, status);
168     }
169 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)170     virtual void NotifyDisplayStateChanged(DisplayId id, DisplayState state) override
171     {
172         pImpl_->NotifyDisplayStateChanged(id, state);
173     }
174 private:
175     sptr<Impl> pImpl_;
176 };
177 
178 class DisplayManager::Impl::DisplayManagerScreenshotAgent : public DisplayManagerAgentDefault {
179 public:
DisplayManagerScreenshotAgent(sptr<Impl> impl)180     explicit DisplayManagerScreenshotAgent(sptr<Impl> impl) : pImpl_(impl)
181     {
182     }
183     ~DisplayManagerScreenshotAgent() = default;
184 
OnScreenshot(sptr<ScreenshotInfo> info)185     virtual void OnScreenshot(sptr<ScreenshotInfo> info) override
186     {
187         pImpl_->NotifyScreenshot(info);
188     }
189 private:
190     sptr<Impl> pImpl_;
191 };
192 
CheckRectValid(const Media::Rect & rect,int32_t oriHeight,int32_t oriWidth) const193 bool DisplayManager::Impl::CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const
194 {
195     if (rect.left < 0) {
196         return false;
197     }
198     if (rect.top < 0) {
199         return false;
200     }
201     if (rect.width < 0) {
202         return false;
203     }
204     if (rect.height < 0) {
205         return false;
206     }
207     if (rect.width + rect.left > oriWidth) {
208         return false;
209     }
210     if (rect.height + rect.top > oriHeight) {
211         return false;
212     }
213     return true;
214 }
215 
CheckSizeValid(const Media::Size & size,int32_t oriHeight,int32_t oriWidth) const216 bool DisplayManager::Impl::CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const
217 {
218     if (size.width < 0) {
219         return false;
220     }
221     if (size.height < 0) {
222         return false;
223     }
224     if (size.width > MAX_RESOLUTION_SIZE_SCREENSHOT) {
225         return false;
226     }
227     if (size.height > MAX_RESOLUTION_SIZE_SCREENSHOT) {
228         return false;
229     }
230     return true;
231 }
232 
ClearDisplayStateCallback()233 void DisplayManager::Impl::ClearDisplayStateCallback()
234 {
235     std::lock_guard<std::recursive_mutex> lock(mutex_);
236     displayStateCallback_ = nullptr;
237     if (displayStateAgent_ != nullptr) {
238         SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(displayStateAgent_,
239             DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
240         displayStateAgent_ = nullptr;
241     }
242 }
243 
Clear()244 void DisplayManager::Impl::Clear()
245 {
246     std::lock_guard<std::recursive_mutex> lock(mutex_);
247     bool res = true;
248     if (displayManagerListener_ != nullptr) {
249         res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
250             displayManagerListener_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
251     }
252     displayManagerListener_ = nullptr;
253     if (!res) {
254         WLOGFW("UnregisterDisplayManagerAgent DISPLAY_EVENT_LISTENER failed !");
255     }
256     res = true;
257     if (powerEventListenerAgent_ != nullptr) {
258         res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
259             powerEventListenerAgent_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
260     }
261     powerEventListenerAgent_ = nullptr;
262     if (!res) {
263         WLOGFW("UnregisterDisplayManagerAgent DISPLAY_POWER_EVENT_LISTENER failed !");
264     }
265     ClearDisplayStateCallback();
266 }
267 
~Impl()268 DisplayManager::Impl::~Impl()
269 {
270     Clear();
271 }
272 
DisplayManager()273 DisplayManager::DisplayManager() : pImpl_(new Impl(mutex_))
274 {
275 }
276 
~DisplayManager()277 DisplayManager::~DisplayManager()
278 {
279     std::lock_guard<std::recursive_mutex> lock(mutex_);
280     destroyed_ = true;
281 }
282 
GetDefaultDisplayId()283 DisplayId DisplayManager::GetDefaultDisplayId()
284 {
285     auto info = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
286     if (info == nullptr) {
287         return DISPLAY_ID_INVALID;
288     }
289     return info->GetDisplayId();
290 }
291 
GetDefaultDisplay()292 sptr<Display> DisplayManager::Impl::GetDefaultDisplay()
293 {
294     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
295     if (displayInfo == nullptr) {
296         return nullptr;
297     }
298     auto displayId = displayInfo->GetDisplayId();
299     std::lock_guard<std::recursive_mutex> lock(mutex_);
300     if (!UpdateDisplayInfoLocked(displayInfo)) {
301         displayMap_.erase(displayId);
302         return nullptr;
303     }
304     return displayMap_[displayId];
305 }
306 
GetDefaultDisplaySync()307 sptr<Display> DisplayManager::Impl::GetDefaultDisplaySync()
308 {
309     static std::chrono::steady_clock::time_point lastRequestTime = std::chrono::steady_clock::now();
310     auto currentTime = std::chrono::steady_clock::now();
311     auto interval = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - lastRequestTime).count();
312     if (defaultDisplayId_ != DISPLAY_ID_INVALID && interval < MAX_INTERVAL_US) {
313         std::lock_guard<std::recursive_mutex> lock(mutex_);
314         auto iter = displayMap_.find(defaultDisplayId_);
315         if (iter != displayMap_.end()) {
316             return displayMap_[defaultDisplayId_];
317         }
318     }
319 
320     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayInfo();
321     if (displayInfo == nullptr) {
322         return nullptr;
323     }
324     auto displayId = displayInfo->GetDisplayId();
325     std::lock_guard<std::recursive_mutex> lock(mutex_);
326     if (!UpdateDisplayInfoLocked(displayInfo)) {
327         displayMap_.erase(displayId);
328         return nullptr;
329     }
330     lastRequestTime = currentTime;
331     defaultDisplayId_ = displayId;
332     return displayMap_[displayId];
333 }
334 
GetDisplayById(DisplayId displayId)335 sptr<Display> DisplayManager::Impl::GetDisplayById(DisplayId displayId)
336 {
337     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfo(displayId);
338     std::lock_guard<std::recursive_mutex> lock(mutex_);
339     if (!UpdateDisplayInfoLocked(displayInfo)) {
340         displayMap_.erase(displayId);
341         return nullptr;
342     }
343     return displayMap_[displayId];
344 }
345 
GetDisplayById(DisplayId displayId)346 sptr<Display> DisplayManager::GetDisplayById(DisplayId displayId)
347 {
348     std::lock_guard<std::recursive_mutex> lock(mutex_);
349     if (destroyed_) {
350         return nullptr;
351     }
352     return pImpl_->GetDisplayById(displayId);
353 }
354 
GetDisplayByScreen(ScreenId screenId)355 sptr<Display> DisplayManager::GetDisplayByScreen(ScreenId screenId)
356 {
357     if (screenId == SCREEN_ID_INVALID) {
358         WLOGFE("screenId is invalid.");
359         return nullptr;
360     }
361     sptr<Display> display = pImpl_->GetDisplayByScreenId(screenId);
362     if (display == nullptr) {
363         WLOGFE("get display by screenId failed. screen %{public}" PRIu64"", screenId);
364     }
365     return display;
366 }
367 
GetDisplayByScreenId(ScreenId screenId)368 sptr<Display> DisplayManager::Impl::GetDisplayByScreenId(ScreenId screenId)
369 {
370     sptr<DisplayInfo> displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfoByScreenId(screenId);
371     if (displayInfo == nullptr) {
372         WLOGFE("get display by screenId: displayInfo is null");
373         return nullptr;
374     }
375     DisplayId displayId = displayInfo->GetDisplayId();
376     if (displayId == DISPLAY_ID_INVALID) {
377         WLOGFE("get display by screenId: invalid displayInfo");
378         return nullptr;
379     }
380 
381     std::lock_guard<std::recursive_mutex> lock(mutex_);
382     if (!UpdateDisplayInfoLocked(displayInfo)) {
383         displayMap_.erase(displayId);
384         return nullptr;
385     }
386     return displayMap_[displayId];
387 }
388 
GetScreenshot(DisplayId displayId)389 std::shared_ptr<Media::PixelMap> DisplayManager::GetScreenshot(DisplayId displayId)
390 {
391     if (displayId == DISPLAY_ID_INVALID) {
392         WLOGFE("displayId invalid!");
393         return nullptr;
394     }
395     std::shared_ptr<Media::PixelMap> screenShot =
396         SingletonContainer::Get<DisplayManagerAdapter>().GetDisplaySnapshot(displayId);
397     if (screenShot == nullptr) {
398         WLOGFE("DisplayManager::GetScreenshot failed!");
399         return nullptr;
400     }
401 
402     return screenShot;
403 }
404 
GetScreenshot(DisplayId displayId,const Media::Rect & rect,const Media::Size & size,int rotation)405 std::shared_ptr<Media::PixelMap> DisplayManager::GetScreenshot(DisplayId displayId, const Media::Rect &rect,
406                                                                const Media::Size &size, int rotation)
407 {
408     if (displayId == DISPLAY_ID_INVALID) {
409         WLOGFE("displayId invalid!");
410         return nullptr;
411     }
412 
413     std::shared_ptr<Media::PixelMap> screenShot =
414         SingletonContainer::Get<DisplayManagerAdapter>().GetDisplaySnapshot(displayId);
415     if (screenShot == nullptr) {
416         WLOGFE("DisplayManager::GetScreenshot failed!");
417         return nullptr;
418     }
419 
420     // check parameters
421     int32_t oriHeight = screenShot->GetHeight();
422     int32_t oriWidth = screenShot->GetWidth();
423     if (!pImpl_->CheckRectValid(rect, oriHeight, oriWidth)) {
424         WLOGFE("rect invalid! left %{public}d, top %{public}d, w %{public}d, h %{public}d",
425             rect.left, rect.top, rect.width, rect.height);
426         return nullptr;
427     }
428     if (!pImpl_->CheckSizeValid(size, oriHeight, oriWidth)) {
429         WLOGFE("size invalid! w %{public}d, h %{public}d", rect.width, rect.height);
430         return nullptr;
431     }
432 
433     // create crop dest pixelmap
434     Media::InitializationOptions opt;
435     opt.size.width = size.width;
436     opt.size.height = size.height;
437     opt.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
438     opt.editable = false;
439     auto pixelMap = Media::PixelMap::Create(*screenShot, rect, opt);
440     if (pixelMap == nullptr) {
441         WLOGFE("Media::PixelMap::Create failed!");
442         return nullptr;
443     }
444     std::shared_ptr<Media::PixelMap> dstScreenshot(pixelMap.release());
445 
446     return dstScreenshot;
447 }
448 
GetDefaultDisplay()449 sptr<Display> DisplayManager::GetDefaultDisplay()
450 {
451     return pImpl_->GetDefaultDisplay();
452 }
453 
GetDefaultDisplaySync()454 sptr<Display> DisplayManager::GetDefaultDisplaySync()
455 {
456     return pImpl_->GetDefaultDisplaySync();
457 }
458 
GetAllDisplayIds()459 std::vector<DisplayId> DisplayManager::GetAllDisplayIds()
460 {
461     return SingletonContainer::Get<DisplayManagerAdapter>().GetAllDisplayIds();
462 }
463 
GetAllDisplays()464 std::vector<sptr<Display>> DisplayManager::GetAllDisplays()
465 {
466     std::vector<sptr<Display>> res;
467     auto displayIds = GetAllDisplayIds();
468     for (auto displayId: displayIds) {
469         const sptr<Display> display = GetDisplayById(displayId);
470         if (display != nullptr) {
471             res.emplace_back(display);
472         } else {
473             WLOGFE("DisplayManager::GetAllDisplays display %" PRIu64" nullptr!", displayId);
474         }
475     }
476     return res;
477 }
478 
HasPrivateWindow(DisplayId displayId,bool & hasPrivateWindow)479 DMError DisplayManager::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
480 {
481     return pImpl_->HasPrivateWindow(displayId, hasPrivateWindow);
482 }
483 
HasPrivateWindow(DisplayId displayId,bool & hasPrivateWindow)484 DMError DisplayManager::Impl::HasPrivateWindow(DisplayId displayId, bool& hasPrivateWindow)
485 {
486     return SingletonContainer::Get<DisplayManagerAdapter>().HasPrivateWindow(displayId, hasPrivateWindow);
487 }
488 
RegisterDisplayListener(sptr<IDisplayListener> listener)489 bool DisplayManager::Impl::RegisterDisplayListener(sptr<IDisplayListener> listener)
490 {
491     std::lock_guard<std::recursive_mutex> lock(mutex_);
492     bool ret = true;
493     if (displayManagerListener_ == nullptr) {
494         displayManagerListener_ = new DisplayManagerListener(this);
495         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
496             displayManagerListener_,
497             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
498     }
499     if (!ret) {
500         WLOGFW("RegisterDisplayManagerAgent failed !");
501         displayManagerListener_ = nullptr;
502     } else {
503         displayListeners_.insert(listener);
504     }
505     return ret;
506 }
507 
RegisterDisplayListener(sptr<IDisplayListener> listener)508 bool DisplayManager::RegisterDisplayListener(sptr<IDisplayListener> listener)
509 {
510     if (listener == nullptr) {
511         WLOGFE("RegisterDisplayListener listener is nullptr.");
512         return false;
513     }
514     return pImpl_->RegisterDisplayListener(listener);
515 }
516 
UnregisterDisplayListener(sptr<IDisplayListener> listener)517 bool DisplayManager::Impl::UnregisterDisplayListener(sptr<IDisplayListener> listener)
518 {
519     std::lock_guard<std::recursive_mutex> lock(mutex_);
520     auto iter = std::find(displayListeners_.begin(), displayListeners_.end(), listener);
521     if (iter == displayListeners_.end()) {
522         WLOGFE("could not find this listener");
523         return false;
524     }
525     displayListeners_.erase(iter);
526     bool ret = true;
527     if (displayListeners_.empty() && displayManagerListener_ != nullptr) {
528         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
529             displayManagerListener_,
530             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
531         displayManagerListener_ = nullptr;
532     }
533     return ret;
534 }
535 
UnregisterDisplayListener(sptr<IDisplayListener> listener)536 bool DisplayManager::UnregisterDisplayListener(sptr<IDisplayListener> listener)
537 {
538     if (listener == nullptr) {
539         WLOGFE("UnregisterDisplayListener listener is nullptr.");
540         return false;
541     }
542     return pImpl_->UnregisterDisplayListener(listener);
543 }
544 
RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)545 bool DisplayManager::Impl::RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
546 {
547     std::lock_guard<std::recursive_mutex> lock(mutex_);
548     bool ret = true;
549     if (powerEventListenerAgent_ == nullptr) {
550         powerEventListenerAgent_ = new DisplayManagerAgent(this);
551         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
552             powerEventListenerAgent_,
553             DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
554     }
555     if (!ret) {
556         WLOGFW("RegisterDisplayManagerAgent failed !");
557         powerEventListenerAgent_ = nullptr;
558     } else {
559         powerEventListeners_.insert(listener);
560     }
561     WLOGFD("RegisterDisplayPowerEventListener end");
562     return ret;
563 }
564 
RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)565 bool DisplayManager::RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
566 {
567     if (listener == nullptr) {
568         WLOGFE("listener is nullptr");
569         return false;
570     }
571     return pImpl_->RegisterDisplayPowerEventListener(listener);
572 }
573 
UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)574 bool DisplayManager::Impl::UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
575 {
576     std::lock_guard<std::recursive_mutex> lock(mutex_);
577     auto iter = std::find(powerEventListeners_.begin(), powerEventListeners_.end(), listener);
578     if (iter == powerEventListeners_.end()) {
579         WLOGFE("could not find this listener");
580         return false;
581     }
582     powerEventListeners_.erase(iter);
583     bool ret = true;
584     if (powerEventListeners_.empty() && powerEventListenerAgent_ != nullptr) {
585         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
586             powerEventListenerAgent_,
587             DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
588         powerEventListenerAgent_ = nullptr;
589     }
590     WLOGFD("UnregisterDisplayPowerEventListener end");
591     return ret;
592 }
593 
UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)594 bool DisplayManager::UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
595 {
596     if (listener == nullptr) {
597         WLOGFE("listener is nullptr");
598         return false;
599     }
600     return pImpl_->UnregisterDisplayPowerEventListener(listener);
601 }
602 
RegisterScreenshotListener(sptr<IScreenshotListener> listener)603 bool DisplayManager::Impl::RegisterScreenshotListener(sptr<IScreenshotListener> listener)
604 {
605     std::lock_guard<std::recursive_mutex> lock(mutex_);
606     bool ret = true;
607     if (screenshotListenerAgent_ == nullptr) {
608         screenshotListenerAgent_ = new DisplayManagerScreenshotAgent(this);
609         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
610             screenshotListenerAgent_,
611             DisplayManagerAgentType::SCREENSHOT_EVENT_LISTENER);
612     }
613     if (!ret) {
614         WLOGFW("RegisterDisplayManagerAgent failed !");
615         screenshotListenerAgent_ = nullptr;
616     } else {
617         screenshotListeners_.insert(listener);
618     }
619     return ret;
620 }
621 
RegisterScreenshotListener(sptr<IScreenshotListener> listener)622 bool DisplayManager::RegisterScreenshotListener(sptr<IScreenshotListener> listener)
623 {
624     if (listener == nullptr) {
625         WLOGFE("RegisterScreenshotListener listener is nullptr.");
626         return false;
627     }
628     return pImpl_->RegisterScreenshotListener(listener);
629 }
630 
UnregisterScreenshotListener(sptr<IScreenshotListener> listener)631 bool DisplayManager::Impl::UnregisterScreenshotListener(sptr<IScreenshotListener> listener)
632 {
633     std::lock_guard<std::recursive_mutex> lock(mutex_);
634     auto iter = std::find(screenshotListeners_.begin(), screenshotListeners_.end(), listener);
635     if (iter == screenshotListeners_.end()) {
636         WLOGFE("could not find this listener");
637         return false;
638     }
639     screenshotListeners_.erase(iter);
640     bool ret = true;
641     if (screenshotListeners_.empty() && screenshotListenerAgent_ != nullptr) {
642         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
643             screenshotListenerAgent_,
644             DisplayManagerAgentType::SCREENSHOT_EVENT_LISTENER);
645         screenshotListenerAgent_ = nullptr;
646     }
647     return ret;
648 }
649 
UnregisterScreenshotListener(sptr<IScreenshotListener> listener)650 bool DisplayManager::UnregisterScreenshotListener(sptr<IScreenshotListener> listener)
651 {
652     if (listener == nullptr) {
653         WLOGFE("UnregisterScreenshotListener listener is nullptr.");
654         return false;
655     }
656     return pImpl_->UnregisterScreenshotListener(listener);
657 }
658 
NotifyScreenshot(sptr<ScreenshotInfo> info)659 void DisplayManager::Impl::NotifyScreenshot(sptr<ScreenshotInfo> info)
660 {
661     WLOGFD("NotifyScreenshot trigger:[%{public}s] displayId:%{public}" PRIu64" size:%{public}zu",
662         info->GetTrigger().c_str(), info->GetDisplayId(), screenshotListeners_.size());
663     std::set<sptr<IScreenshotListener>> screenshotListeners;
664     {
665         std::lock_guard<std::recursive_mutex> lock(mutex_);
666         screenshotListeners = screenshotListeners_;
667     }
668     for (auto& listener : screenshotListeners) {
669         listener->OnScreenshot(*info);
670     }
671 }
672 
NotifyDisplayPowerEvent(DisplayPowerEvent event,EventStatus status)673 void DisplayManager::Impl::NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status)
674 {
675     WLOGFD("NotifyDisplayPowerEvent event:%{public}u, status:%{public}u, size:%{public}zu", event, status,
676         powerEventListeners_.size());
677     std::set<sptr<IDisplayPowerEventListener>> powerEventListeners;
678     {
679         std::lock_guard<std::recursive_mutex> lock(mutex_);
680         powerEventListeners = powerEventListeners_;
681     }
682     for (auto& listener : powerEventListeners) {
683         listener->OnDisplayPowerEvent(event, status);
684     }
685 }
686 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)687 void DisplayManager::Impl::NotifyDisplayStateChanged(DisplayId id, DisplayState state)
688 {
689     WLOGFD("state:%{public}u", state);
690     DisplayStateCallback displayStateCallback;
691     {
692         std::lock_guard<std::recursive_mutex> lock(mutex_);
693         displayStateCallback = displayStateCallback_;
694     }
695     if (displayStateCallback) {
696         displayStateCallback(state);
697         ClearDisplayStateCallback();
698         return;
699     }
700     WLOGFW("callback_ target is not set!");
701 }
702 
NotifyDisplayCreate(sptr<DisplayInfo> info)703 void DisplayManager::Impl::NotifyDisplayCreate(sptr<DisplayInfo> info)
704 {
705     std::lock_guard<std::recursive_mutex> lock(mutex_);
706     UpdateDisplayInfoLocked(info);
707 }
708 
NotifyDisplayDestroy(DisplayId displayId)709 void DisplayManager::Impl::NotifyDisplayDestroy(DisplayId displayId)
710 {
711     WLOGFD("displayId:%{public}" PRIu64".", displayId);
712     std::lock_guard<std::recursive_mutex> lock(mutex_);
713     displayMap_.erase(displayId);
714 }
715 
NotifyDisplayChange(sptr<DisplayInfo> displayInfo)716 void DisplayManager::Impl::NotifyDisplayChange(sptr<DisplayInfo> displayInfo)
717 {
718     std::lock_guard<std::recursive_mutex> lock(mutex_);
719     UpdateDisplayInfoLocked(displayInfo);
720 }
721 
UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)722 bool DisplayManager::Impl::UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)
723 {
724     if (displayInfo == nullptr) {
725         WLOGFE("displayInfo is null");
726         return false;
727     }
728     DisplayId displayId = displayInfo->GetDisplayId();
729     WLOGFD("displayId:%{public}" PRIu64".", displayId);
730     if (displayId == DISPLAY_ID_INVALID) {
731         WLOGFE("displayId is invalid.");
732         return false;
733     }
734     auto iter = displayMap_.find(displayId);
735     if (iter != displayMap_.end() && iter->second != nullptr) {
736         WLOGFD("get screen in screen map");
737         iter->second->UpdateDisplayInfo(displayInfo);
738         return true;
739     }
740     sptr<Display> display = new Display("", displayInfo);
741     displayMap_[displayId] = display;
742     return true;
743 }
744 
WakeUpBegin(PowerStateChangeReason reason)745 bool DisplayManager::WakeUpBegin(PowerStateChangeReason reason)
746 {
747     WLOGFD("WakeUpBegin start, reason:%{public}u", reason);
748     return SingletonContainer::Get<DisplayManagerAdapter>().WakeUpBegin(reason);
749 }
750 
WakeUpEnd()751 bool DisplayManager::WakeUpEnd()
752 {
753     WLOGFD("WakeUpEnd start");
754     return SingletonContainer::Get<DisplayManagerAdapter>().WakeUpEnd();
755 }
756 
SuspendBegin(PowerStateChangeReason reason)757 bool DisplayManager::SuspendBegin(PowerStateChangeReason reason)
758 {
759     // dms->wms notify other windows to hide
760     WLOGFD("SuspendBegin start, reason:%{public}u", reason);
761     return SingletonContainer::Get<DisplayManagerAdapter>().SuspendBegin(reason);
762 }
763 
SuspendEnd()764 bool DisplayManager::SuspendEnd()
765 {
766     WLOGFD("SuspendEnd start");
767     return SingletonContainer::Get<DisplayManagerAdapter>().SuspendEnd();
768 }
769 
SetDisplayState(DisplayState state,DisplayStateCallback callback)770 bool DisplayManager::Impl::SetDisplayState(DisplayState state, DisplayStateCallback callback)
771 {
772     WLOGFD("state:%{public}u", state);
773     bool ret = true;
774     {
775         std::lock_guard<std::recursive_mutex> lock(mutex_);
776         if (displayStateCallback_ != nullptr || callback == nullptr) {
777             WLOGFI("previous callback not called or callback invalid");
778             return false;
779         }
780         displayStateCallback_ = callback;
781 
782         if (displayStateAgent_ == nullptr) {
783             displayStateAgent_ = new DisplayManagerAgent(this);
784             ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
785                 displayStateAgent_,
786                 DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
787         }
788     }
789     ret = ret && SingletonContainer::Get<DisplayManagerAdapter>().SetDisplayState(state);
790     if (!ret) {
791         ClearDisplayStateCallback();
792     }
793     return ret;
794 }
795 
SetDisplayState(DisplayState state,DisplayStateCallback callback)796 bool DisplayManager::SetDisplayState(DisplayState state, DisplayStateCallback callback)
797 {
798     return pImpl_->SetDisplayState(state, callback);
799 }
800 
GetDisplayState(DisplayId displayId)801 DisplayState DisplayManager::GetDisplayState(DisplayId displayId)
802 {
803     return SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayState(displayId);
804 }
805 
SetScreenBrightness(uint64_t screenId,uint32_t level)806 bool DisplayManager::SetScreenBrightness(uint64_t screenId, uint32_t level)
807 {
808     WLOGFD("screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
809     RSInterfaces::GetInstance().SetScreenBacklight(screenId, level);
810     return true;
811 }
812 
GetScreenBrightness(uint64_t screenId) const813 uint32_t DisplayManager::GetScreenBrightness(uint64_t screenId) const
814 {
815     uint32_t level = static_cast<uint32_t>(RSInterfaces::GetInstance().GetScreenBacklight(screenId));
816     WLOGFD("screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
817     return level;
818 }
819 
NotifyDisplayEvent(DisplayEvent event)820 void DisplayManager::NotifyDisplayEvent(DisplayEvent event)
821 {
822     // Unlock event dms->wms restore other hidden windows
823     WLOGFD("DisplayEvent:%{public}u", event);
824     SingletonContainer::Get<DisplayManagerAdapter>().NotifyDisplayEvent(event);
825 }
826 
Freeze(std::vector<DisplayId> displayIds)827 bool DisplayManager::Freeze(std::vector<DisplayId> displayIds)
828 {
829     WLOGFD("freeze display");
830     if (displayIds.size() == 0) {
831         WLOGFE("freeze display fail, num of display is 0");
832         return false;
833     }
834     if (displayIds.size() > MAX_DISPLAY_SIZE) {
835         WLOGFE("freeze display fail, displayIds size is bigger than %{public}u.", MAX_DISPLAY_SIZE);
836         return false;
837     }
838     return SingletonContainer::Get<DisplayManagerAdapter>().SetFreeze(displayIds, true);
839 }
840 
Unfreeze(std::vector<DisplayId> displayIds)841 bool DisplayManager::Unfreeze(std::vector<DisplayId> displayIds)
842 {
843     WLOGFD("unfreeze display");
844     if (displayIds.size() == 0) {
845         WLOGFE("unfreeze display fail, num of display is 0");
846         return false;
847     }
848     if (displayIds.size() > MAX_DISPLAY_SIZE) {
849         WLOGFE("unfreeze display fail, displayIds size is bigger than %{public}u.", MAX_DISPLAY_SIZE);
850         return false;
851     }
852     return SingletonContainer::Get<DisplayManagerAdapter>().SetFreeze(displayIds, false);
853 }
854 
OnRemoteDied()855 void DisplayManager::Impl::OnRemoteDied()
856 {
857     WLOGFI("dms is died");
858     std::lock_guard<std::recursive_mutex> lock(mutex_);
859     displayManagerListener_ = nullptr;
860     displayStateAgent_ = nullptr;
861     powerEventListenerAgent_ = nullptr;
862     screenshotListenerAgent_ = nullptr;
863 }
864 
OnRemoteDied()865 void DisplayManager::OnRemoteDied()
866 {
867     pImpl_->OnRemoteDied();
868 }
869 } // namespace OHOS::Rosen