• 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 <cinttypes>
19 #include <transaction/rs_interfaces.h>
20 
21 #include "display_manager_adapter.h"
22 #include "display_manager_agent_default.h"
23 #include "dm_common.h"
24 #include "screen_manager.h"
25 #include "singleton_delegator.h"
26 #include "window_manager_hilog.h"
27 
28 namespace OHOS::Rosen {
29 namespace {
30     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManager"};
31     const static uint32_t MAX_DISPLAY_SIZE = 32;
32 }
33 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManager)
34 
35 class DisplayManager::Impl : public RefBase {
36 public:
37     ~Impl();
38     static inline SingletonDelegator<DisplayManager> delegator;
39     bool CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const;
40     bool CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const;
41     sptr<Display> GetDisplayById(DisplayId displayId);
42     bool RegisterDisplayListener(sptr<IDisplayListener> listener);
43     bool UnregisterDisplayListener(sptr<IDisplayListener> listener);
44     bool SetDisplayState(DisplayState state, DisplayStateCallback callback);
45     bool RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener);
46     bool UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener);
47     sptr<Display> GetDisplayByScreenId(ScreenId screenId);
48 private:
49     void ClearDisplayStateCallback();
50     void NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status);
51     void NotifyDisplayStateChanged(DisplayId id, DisplayState state);
52     void NotifyDisplayChangedEvent(sptr<DisplayInfo> info, DisplayChangeEvent event);
53     void NotifyDisplayCreate(sptr<DisplayInfo> info);
54     void NotifyDisplayDestroy(DisplayId);
55     void NotifyDisplayChange(sptr<DisplayInfo> displayInfo);
56     bool UpdateDisplayInfoLocked(sptr<DisplayInfo>);
57 
58     class DisplayManagerListener;
59     sptr<DisplayManagerListener> displayManagerListener_;
60     std::map<DisplayId, sptr<Display>> displayMap_;
61     DisplayStateCallback displayStateCallback_;
62     std::recursive_mutex mutex_;
63     std::set<sptr<IDisplayPowerEventListener>> powerEventListeners_;
64     class DisplayManagerAgent;
65     sptr<DisplayManagerAgent> powerEventListenerAgent_;
66     sptr<DisplayManagerAgent> displayStateAgent_;
67     std::set<sptr<IDisplayListener>> displayListeners_;
68 };
69 
70 class DisplayManager::Impl::DisplayManagerListener : public DisplayManagerAgentDefault {
71 public:
DisplayManagerListener(sptr<Impl> impl)72     DisplayManagerListener(sptr<Impl> impl) : pImpl_(impl)
73     {
74     }
75 
OnDisplayCreate(sptr<DisplayInfo> displayInfo)76     void OnDisplayCreate(sptr<DisplayInfo> displayInfo) override
77     {
78         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
79             WLOGFE("OnDisplayCreate, displayInfo is invalid.");
80             return;
81         }
82         if (pImpl_ == nullptr) {
83             WLOGFE("OnDisplayCreate, impl is nullptr.");
84             return;
85         }
86         pImpl_->NotifyDisplayCreate(displayInfo);
87         for (auto listener : pImpl_->displayListeners_) {
88             listener->OnCreate(displayInfo->GetDisplayId());
89         }
90     };
91 
OnDisplayDestroy(DisplayId displayId)92     void OnDisplayDestroy(DisplayId displayId) override
93     {
94         if (displayId == DISPLAY_ID_INVALID) {
95             WLOGFE("OnDisplayDestroy, displayId is invalid.");
96             return;
97         }
98         if (pImpl_ == nullptr) {
99             WLOGFE("OnDisplayDestroy, impl is nullptr.");
100             return;
101         }
102         pImpl_->NotifyDisplayDestroy(displayId);
103         for (auto listener : pImpl_->displayListeners_) {
104             listener->OnDestroy(displayId);
105         }
106     };
107 
OnDisplayChange(sptr<DisplayInfo> displayInfo,DisplayChangeEvent event)108     void OnDisplayChange(sptr<DisplayInfo> displayInfo, DisplayChangeEvent event) override
109     {
110         if (displayInfo == nullptr || displayInfo->GetDisplayId() == DISPLAY_ID_INVALID) {
111             WLOGFE("OnDisplayChange, displayInfo is invalid.");
112             return;
113         }
114         if (pImpl_ == nullptr) {
115             WLOGFE("OnDisplayChange, impl is nullptr.");
116             return;
117         }
118         WLOGD("OnDisplayChange. display %{public}" PRIu64", event %{public}u", displayInfo->GetDisplayId(), event);
119         pImpl_->NotifyDisplayChange(displayInfo);
120         for (auto listener : pImpl_->displayListeners_) {
121             listener->OnChange(displayInfo->GetDisplayId());
122         }
123     };
124 private:
125     sptr<Impl> pImpl_;
126 };
127 
128 class DisplayManager::Impl::DisplayManagerAgent : public DisplayManagerAgentDefault {
129 public:
DisplayManagerAgent(sptr<Impl> impl)130     DisplayManagerAgent(sptr<Impl> impl) : pImpl_(impl)
131     {
132     }
133     ~DisplayManagerAgent() = default;
134 
NotifyDisplayPowerEvent(DisplayPowerEvent event,EventStatus status)135     virtual void NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status) override
136     {
137         pImpl_->NotifyDisplayPowerEvent(event, status);
138     }
139 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)140     virtual void NotifyDisplayStateChanged(DisplayId id, DisplayState state) override
141     {
142         pImpl_->NotifyDisplayStateChanged(id, state);
143     }
144 private:
145     sptr<Impl> pImpl_;
146 };
147 
CheckRectValid(const Media::Rect & rect,int32_t oriHeight,int32_t oriWidth) const148 bool DisplayManager::Impl::CheckRectValid(const Media::Rect& rect, int32_t oriHeight, int32_t oriWidth) const
149 {
150     if (!((rect.left >= 0) && (rect.left < oriWidth) && (rect.top >= 0) && (rect.top < oriHeight))) {
151         WLOGFE("rect left or top invalid!");
152         return false;
153     }
154 
155     if (!((rect.width > 0) && (rect.width <= (oriWidth - rect.left)) &&
156         (rect.height > 0) && (rect.height <= (oriHeight - rect.top)))) {
157         if (!((rect.width == 0) && (rect.height == 0))) {
158             WLOGFE("rect height or width invalid!");
159             return false;
160         }
161     }
162     return true;
163 }
164 
CheckSizeValid(const Media::Size & size,int32_t oriHeight,int32_t oriWidth) const165 bool DisplayManager::Impl::CheckSizeValid(const Media::Size& size, int32_t oriHeight, int32_t oriWidth) const
166 {
167     if (!((size.width > 0) && (size.height > 0))) {
168         if (!((size.width == 0) && (size.height == 0))) {
169             WLOGFE("width or height invalid!");
170             return false;
171         }
172     }
173 
174     if ((size.width > MAX_RESOLUTION_SIZE_SCREENSHOT) or (size.height > MAX_RESOLUTION_SIZE_SCREENSHOT)) {
175         WLOGFE("width or height too big!");
176         return false;
177     }
178     return true;
179 }
180 
ClearDisplayStateCallback()181 void DisplayManager::Impl::ClearDisplayStateCallback()
182 {
183     std::lock_guard<std::recursive_mutex> lock(mutex_);
184     displayStateCallback_ = nullptr;
185     if (displayStateAgent_ != nullptr) {
186         SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(displayStateAgent_,
187             DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
188         displayStateAgent_ = nullptr;
189     }
190 }
191 
~Impl()192 DisplayManager::Impl::~Impl()
193 {
194     std::lock_guard<std::recursive_mutex> lock(mutex_);
195     bool res = true;
196     if (displayManagerListener_ != nullptr) {
197         res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
198             displayManagerListener_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
199     }
200     displayManagerListener_ = nullptr;
201     if (!res) {
202         WLOGFW("UnregisterDisplayManagerAgent DISPLAY_EVENT_LISTENER failed !");
203     }
204     res = true;
205     if (powerEventListenerAgent_ != nullptr) {
206         res = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
207             powerEventListenerAgent_, DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
208     }
209     powerEventListenerAgent_ = nullptr;
210     if (!res) {
211         WLOGFW("UnregisterDisplayManagerAgent DISPLAY_POWER_EVENT_LISTENER failed !");
212     }
213     ClearDisplayStateCallback();
214 }
215 
DisplayManager()216 DisplayManager::DisplayManager() : pImpl_(new Impl())
217 {
218 }
219 
~DisplayManager()220 DisplayManager::~DisplayManager()
221 {
222 }
223 
GetDefaultDisplayId()224 DisplayId DisplayManager::GetDefaultDisplayId()
225 {
226     return SingletonContainer::Get<DisplayManagerAdapter>().GetDefaultDisplayId();
227 }
228 
GetDisplayById(DisplayId displayId)229 sptr<Display> DisplayManager::Impl::GetDisplayById(DisplayId displayId)
230 {
231     auto displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfo(displayId);
232     std::lock_guard<std::recursive_mutex> lock(mutex_);
233     if (!UpdateDisplayInfoLocked(displayInfo)) {
234         displayMap_.erase(displayId);
235         return nullptr;
236     }
237     return displayMap_[displayId];
238 }
239 
GetDisplayById(DisplayId displayId)240 sptr<Display> DisplayManager::GetDisplayById(DisplayId displayId)
241 {
242     return pImpl_->GetDisplayById(displayId);
243 }
244 
GetDisplayByScreen(ScreenId screenId)245 sptr<Display> DisplayManager::GetDisplayByScreen(ScreenId screenId)
246 {
247     if (screenId == SCREEN_ID_INVALID) {
248         WLOGFE("screenId is invalid.");
249         return nullptr;
250     }
251     sptr<Display> display = pImpl_->GetDisplayByScreenId(screenId);
252     if (display == nullptr) {
253         WLOGFE("get display by screenId failed. screen %{public}" PRIu64"", screenId);
254     }
255     return display;
256 }
257 
GetDisplayByScreenId(ScreenId screenId)258 sptr<Display> DisplayManager::Impl::GetDisplayByScreenId(ScreenId screenId)
259 {
260     sptr<DisplayInfo> displayInfo = SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayInfoByScreenId(screenId);
261     if (displayInfo == nullptr) {
262         WLOGFE("get display by screenId: displayInfo is null");
263         return nullptr;
264     }
265     DisplayId displayId = displayInfo->GetDisplayId();
266     if (displayId == DISPLAY_ID_INVALID) {
267         WLOGFE("get display by screenId: invalid displayInfo");
268         return nullptr;
269     }
270 
271     std::lock_guard<std::recursive_mutex> lock(mutex_);
272     if (!UpdateDisplayInfoLocked(displayInfo)) {
273         displayMap_.erase(displayId);
274         return nullptr;
275     }
276     return displayMap_[displayId];
277 }
278 
GetScreenshot(DisplayId displayId)279 std::shared_ptr<Media::PixelMap> DisplayManager::GetScreenshot(DisplayId displayId)
280 {
281     if (displayId == DISPLAY_ID_INVALID) {
282         WLOGFE("displayId invalid!");
283         return nullptr;
284     }
285     std::shared_ptr<Media::PixelMap> screenShot =
286         SingletonContainer::Get<DisplayManagerAdapter>().GetDisplaySnapshot(displayId);
287     if (screenShot == nullptr) {
288         WLOGFE("DisplayManager::GetScreenshot failed!");
289         return nullptr;
290     }
291 
292     return screenShot;
293 }
294 
GetScreenshot(DisplayId displayId,const Media::Rect & rect,const Media::Size & size,int rotation)295 std::shared_ptr<Media::PixelMap> DisplayManager::GetScreenshot(DisplayId displayId, const Media::Rect &rect,
296                                                                const Media::Size &size, int rotation)
297 {
298     if (displayId == DISPLAY_ID_INVALID) {
299         WLOGFE("displayId invalid!");
300         return nullptr;
301     }
302 
303     std::shared_ptr<Media::PixelMap> screenShot =
304         SingletonContainer::Get<DisplayManagerAdapter>().GetDisplaySnapshot(displayId);
305     if (screenShot == nullptr) {
306         WLOGFE("DisplayManager::GetScreenshot failed!");
307         return nullptr;
308     }
309 
310     // check parameters
311     int32_t oriHeight = screenShot->GetHeight();
312     int32_t oriWidth = screenShot->GetWidth();
313     if (!pImpl_->CheckRectValid(rect, oriHeight, oriWidth)) {
314         WLOGFE("rect invalid! left %{public}d, top %{public}d, w %{public}d, h %{public}d",
315             rect.left, rect.top, rect.width, rect.height);
316         return nullptr;
317     }
318     if (!pImpl_->CheckSizeValid(size, oriHeight, oriWidth)) {
319         WLOGFE("size invalid! w %{public}d, h %{public}d", rect.width, rect.height);
320         return nullptr;
321     }
322 
323     // create crop dest pixelmap
324     Media::InitializationOptions opt;
325     opt.size.width = size.width;
326     opt.size.height = size.height;
327     opt.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
328     opt.editable = false;
329     auto pixelMap = Media::PixelMap::Create(*screenShot, rect, opt);
330     if (pixelMap == nullptr) {
331         WLOGFE("Media::PixelMap::Create failed!");
332         return nullptr;
333     }
334     std::shared_ptr<Media::PixelMap> dstScreenshot(pixelMap.release());
335 
336     return dstScreenshot;
337 }
338 
GetDefaultDisplay()339 sptr<Display> DisplayManager::GetDefaultDisplay()
340 {
341     return GetDisplayById(GetDefaultDisplayId());
342 }
343 
GetAllDisplayIds()344 std::vector<DisplayId> DisplayManager::GetAllDisplayIds()
345 {
346     return SingletonContainer::Get<DisplayManagerAdapter>().GetAllDisplayIds();
347 }
348 
GetAllDisplays()349 std::vector<sptr<Display>> DisplayManager::GetAllDisplays()
350 {
351     std::vector<sptr<Display>> res;
352     auto displayIds = GetAllDisplayIds();
353     for (auto displayId: displayIds) {
354         const sptr<Display> display = GetDisplayById(displayId);
355         if (display != nullptr) {
356             res.emplace_back(display);
357         } else {
358             WLOGFE("DisplayManager::GetAllDisplays display %" PRIu64" nullptr!", displayId);
359         }
360     }
361     return res;
362 }
363 
RegisterDisplayListener(sptr<IDisplayListener> listener)364 bool DisplayManager::Impl::RegisterDisplayListener(sptr<IDisplayListener> listener)
365 {
366     std::lock_guard<std::recursive_mutex> lock(mutex_);
367     bool ret = true;
368     if (displayManagerListener_ == nullptr) {
369         displayManagerListener_ = new DisplayManagerListener(this);
370         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
371             displayManagerListener_,
372             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
373     }
374     if (!ret) {
375         WLOGFW("RegisterDisplayManagerAgent failed !");
376         displayManagerListener_ = nullptr;
377     } else {
378         displayListeners_.insert(listener);
379     }
380     return ret;
381 }
382 
RegisterDisplayListener(sptr<IDisplayListener> listener)383 bool DisplayManager::RegisterDisplayListener(sptr<IDisplayListener> listener)
384 {
385     if (listener == nullptr) {
386         WLOGFE("RegisterDisplayListener listener is nullptr.");
387         return false;
388     }
389     return pImpl_->RegisterDisplayListener(listener);
390 }
391 
UnregisterDisplayListener(sptr<IDisplayListener> listener)392 bool DisplayManager::Impl::UnregisterDisplayListener(sptr<IDisplayListener> listener)
393 {
394     std::lock_guard<std::recursive_mutex> lock(mutex_);
395     auto iter = std::find(displayListeners_.begin(), displayListeners_.end(), listener);
396     if (iter == displayListeners_.end()) {
397         WLOGFE("could not find this listener");
398         return false;
399     }
400     displayListeners_.erase(iter);
401     bool ret = true;
402     if (displayListeners_.empty() && displayManagerListener_ != nullptr) {
403         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
404             displayManagerListener_,
405             DisplayManagerAgentType::DISPLAY_EVENT_LISTENER);
406         displayManagerListener_ = nullptr;
407     }
408     return ret;
409 }
410 
UnregisterDisplayListener(sptr<IDisplayListener> listener)411 bool DisplayManager::UnregisterDisplayListener(sptr<IDisplayListener> listener)
412 {
413     if (listener == nullptr) {
414         WLOGFE("UnregisterDisplayListener listener is nullptr.");
415         return false;
416     }
417     return pImpl_->UnregisterDisplayListener(listener);
418 }
419 
RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)420 bool DisplayManager::Impl::RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
421 {
422     std::lock_guard<std::recursive_mutex> lock(mutex_);
423     bool ret = true;
424     if (powerEventListenerAgent_ == nullptr) {
425         powerEventListenerAgent_ = new DisplayManagerAgent(this);
426         ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
427             powerEventListenerAgent_,
428             DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
429     }
430     if (!ret) {
431         WLOGFW("RegisterDisplayManagerAgent failed !");
432         powerEventListenerAgent_ = nullptr;
433     } else {
434         powerEventListeners_.insert(listener);
435     }
436     WLOGFI("RegisterDisplayPowerEventListener end");
437     return ret;
438 }
439 
RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)440 bool DisplayManager::RegisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
441 {
442     if (listener == nullptr) {
443         WLOGFE("listener is nullptr");
444         return false;
445     }
446     return pImpl_->RegisterDisplayPowerEventListener(listener);
447 }
448 
UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)449 bool DisplayManager::Impl::UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
450 {
451     std::lock_guard<std::recursive_mutex> lock(mutex_);
452     auto iter = std::find(powerEventListeners_.begin(), powerEventListeners_.end(), listener);
453     if (iter == powerEventListeners_.end()) {
454         WLOGFE("could not find this listener");
455         return false;
456     }
457     powerEventListeners_.erase(iter);
458     bool ret = true;
459     if (powerEventListeners_.empty() && powerEventListenerAgent_ != nullptr) {
460         ret = SingletonContainer::Get<DisplayManagerAdapter>().UnregisterDisplayManagerAgent(
461             powerEventListenerAgent_,
462             DisplayManagerAgentType::DISPLAY_POWER_EVENT_LISTENER);
463         powerEventListenerAgent_ = nullptr;
464     }
465     WLOGFI("UnregisterDisplayPowerEventListener end");
466     return ret;
467 }
468 
UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)469 bool DisplayManager::UnregisterDisplayPowerEventListener(sptr<IDisplayPowerEventListener> listener)
470 {
471     if (listener == nullptr) {
472         WLOGFE("listener is nullptr");
473         return false;
474     }
475     return pImpl_->UnregisterDisplayPowerEventListener(listener);
476 }
477 
NotifyDisplayPowerEvent(DisplayPowerEvent event,EventStatus status)478 void DisplayManager::Impl::NotifyDisplayPowerEvent(DisplayPowerEvent event, EventStatus status)
479 {
480     WLOGFI("NotifyDisplayPowerEvent event:%{public}u, status:%{public}u, size:%{public}zu", event, status,
481         powerEventListeners_.size());
482     std::set<sptr<IDisplayPowerEventListener>> powerEventListeners;
483     {
484         std::lock_guard<std::recursive_mutex> lock(mutex_);
485         powerEventListeners = powerEventListeners_;
486     }
487     for (auto& listener : powerEventListeners) {
488         listener->OnDisplayPowerEvent(event, status);
489     }
490 }
491 
NotifyDisplayStateChanged(DisplayId id,DisplayState state)492 void DisplayManager::Impl::NotifyDisplayStateChanged(DisplayId id, DisplayState state)
493 {
494     WLOGFI("state:%{public}u", state);
495     DisplayStateCallback displayStateCallback;
496     {
497         std::lock_guard<std::recursive_mutex> lock(mutex_);
498         displayStateCallback = displayStateCallback_;
499     }
500     if (displayStateCallback) {
501         displayStateCallback(state);
502         ClearDisplayStateCallback();
503         return;
504     }
505     WLOGFW("callback_ target is not set!");
506 }
507 
NotifyDisplayCreate(sptr<DisplayInfo> info)508 void DisplayManager::Impl::NotifyDisplayCreate(sptr<DisplayInfo> info)
509 {
510     std::lock_guard<std::recursive_mutex> lock(mutex_);
511     UpdateDisplayInfoLocked(info);
512 }
513 
NotifyDisplayDestroy(DisplayId displayId)514 void DisplayManager::Impl::NotifyDisplayDestroy(DisplayId displayId)
515 {
516     WLOGFI("displayId:%{public}" PRIu64".", displayId);
517     std::lock_guard<std::recursive_mutex> lock(mutex_);
518     displayMap_.erase(displayId);
519 }
520 
NotifyDisplayChange(sptr<DisplayInfo> displayInfo)521 void DisplayManager::Impl::NotifyDisplayChange(sptr<DisplayInfo> displayInfo)
522 {
523     std::lock_guard<std::recursive_mutex> lock(mutex_);
524     UpdateDisplayInfoLocked(displayInfo);
525 }
526 
UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)527 bool DisplayManager::Impl::UpdateDisplayInfoLocked(sptr<DisplayInfo> displayInfo)
528 {
529     if (displayInfo == nullptr) {
530         WLOGFE("displayInfo is null");
531         return false;
532     }
533     DisplayId displayId = displayInfo->GetDisplayId();
534     WLOGFI("displayId:%{public}" PRIu64".", displayId);
535     if (displayId == DISPLAY_ID_INVALID) {
536         WLOGFE("displayId is invalid.");
537         return false;
538     }
539     auto iter = displayMap_.find(displayId);
540     if (iter != displayMap_.end() && iter->second != nullptr) {
541         WLOGFI("get screen in screen map");
542         iter->second->UpdateDisplayInfo(displayInfo);
543         return true;
544     }
545     sptr<Display> display = new Display("", displayInfo);
546     displayMap_[displayId] = display;
547     return true;
548 }
549 
WakeUpBegin(PowerStateChangeReason reason)550 bool DisplayManager::WakeUpBegin(PowerStateChangeReason reason)
551 {
552     WLOGFI("WakeUpBegin start, reason:%{public}u", reason);
553     return SingletonContainer::Get<DisplayManagerAdapter>().WakeUpBegin(reason);
554 }
555 
WakeUpEnd()556 bool DisplayManager::WakeUpEnd()
557 {
558     WLOGFI("WakeUpEnd start");
559     return SingletonContainer::Get<DisplayManagerAdapter>().WakeUpEnd();
560 }
561 
SuspendBegin(PowerStateChangeReason reason)562 bool DisplayManager::SuspendBegin(PowerStateChangeReason reason)
563 {
564     // dms->wms notify other windows to hide
565     WLOGFI("SuspendBegin start, reason:%{public}u", reason);
566     return SingletonContainer::Get<DisplayManagerAdapter>().SuspendBegin(reason);
567 }
568 
SuspendEnd()569 bool DisplayManager::SuspendEnd()
570 {
571     WLOGFI("SuspendEnd start");
572     return SingletonContainer::Get<DisplayManagerAdapter>().SuspendEnd();
573 }
574 
SetDisplayState(DisplayState state,DisplayStateCallback callback)575 bool DisplayManager::Impl::SetDisplayState(DisplayState state, DisplayStateCallback callback)
576 {
577     WLOGFI("state:%{public}u", state);
578     bool ret = true;
579     {
580         std::lock_guard<std::recursive_mutex> lock(mutex_);
581         if (displayStateCallback_ != nullptr || callback == nullptr) {
582             WLOGFI("previous callback not called or callback invalid");
583             return false;
584         }
585         displayStateCallback_ = callback;
586 
587         if (displayStateAgent_ == nullptr) {
588             displayStateAgent_ = new DisplayManagerAgent(this);
589             ret = SingletonContainer::Get<DisplayManagerAdapter>().RegisterDisplayManagerAgent(
590                 displayStateAgent_,
591                 DisplayManagerAgentType::DISPLAY_STATE_LISTENER);
592         }
593     }
594     ret = ret && SingletonContainer::Get<DisplayManagerAdapter>().SetDisplayState(state);
595     if (!ret) {
596         ClearDisplayStateCallback();
597     }
598     return ret;
599 }
600 
SetDisplayState(DisplayState state,DisplayStateCallback callback)601 bool DisplayManager::SetDisplayState(DisplayState state, DisplayStateCallback callback)
602 {
603     return pImpl_->SetDisplayState(state, callback);
604 }
605 
GetDisplayState(DisplayId displayId)606 DisplayState DisplayManager::GetDisplayState(DisplayId displayId)
607 {
608     return SingletonContainer::Get<DisplayManagerAdapter>().GetDisplayState(displayId);
609 }
610 
SetScreenBrightness(uint64_t screenId,uint32_t level)611 bool DisplayManager::SetScreenBrightness(uint64_t screenId, uint32_t level)
612 {
613     WLOGFI("screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
614     RSInterfaces::GetInstance().SetScreenBacklight(screenId, level);
615     return true;
616 }
617 
GetScreenBrightness(uint64_t screenId) const618 uint32_t DisplayManager::GetScreenBrightness(uint64_t screenId) const
619 {
620     uint32_t level = static_cast<uint32_t>(RSInterfaces::GetInstance().GetScreenBacklight(screenId));
621     WLOGFD("screenId:%{public}" PRIu64", level:%{public}u,", screenId, level);
622     return level;
623 }
624 
NotifyDisplayEvent(DisplayEvent event)625 void DisplayManager::NotifyDisplayEvent(DisplayEvent event)
626 {
627     // Unlock event dms->wms restore other hidden windows
628     WLOGFI("DisplayEvent:%{public}u", event);
629     SingletonContainer::Get<DisplayManagerAdapter>().NotifyDisplayEvent(event);
630 }
631 
Freeze(std::vector<DisplayId> displayIds)632 bool DisplayManager::Freeze(std::vector<DisplayId> displayIds)
633 {
634     WLOGFD("freeze display");
635     if (displayIds.size() == 0) {
636         WLOGFE("freeze display fail, num of display is 0");
637         return false;
638     }
639     if (displayIds.size() > MAX_DISPLAY_SIZE) {
640         WLOGFE("freeze display fail, displayIds size is bigger than %{public}u.", MAX_DISPLAY_SIZE);
641         return false;
642     }
643     return SingletonContainer::Get<DisplayManagerAdapter>().SetFreeze(displayIds, true);
644 }
645 
Unfreeze(std::vector<DisplayId> displayIds)646 bool DisplayManager::Unfreeze(std::vector<DisplayId> displayIds)
647 {
648     WLOGFD("unfreeze display");
649     if (displayIds.size() == 0) {
650         WLOGFE("unfreeze display fail, num of display is 0");
651         return false;
652     }
653     if (displayIds.size() > MAX_DISPLAY_SIZE) {
654         WLOGFE("unfreeze display fail, displayIds size is bigger than %{public}u.", MAX_DISPLAY_SIZE);
655         return false;
656     }
657     return SingletonContainer::Get<DisplayManagerAdapter>().SetFreeze(displayIds, false);
658 }
659 } // namespace OHOS::Rosen