1 /*
2  * Copyright (c) 2023 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 "session_display_power_controller.h"
17 #include "screen_session_manager.h"
18 #include "window_manager_hilog.h"
19 
20 namespace OHOS {
21 namespace Rosen {
SuspendBegin(PowerStateChangeReason reason)22 bool SessionDisplayPowerController::SuspendBegin(PowerStateChangeReason reason)
23 {
24     TLOGI(WmsLogTag::DMS, "reason:%{public}u", reason);
25     std::map<DisplayId, sptr<DisplayInfo>> emptyMap;
26     displayStateChangeListener_(DISPLAY_ID_INVALID, nullptr, emptyMap, DisplayStateChangeType::BEFORE_SUSPEND);
27     return true;
28 }
29 
SetDisplayStateToOn(DisplayState & state)30 void SessionDisplayPowerController::SetDisplayStateToOn(DisplayState& state)
31 {
32     displayState_ = state;
33     if (!ScreenSessionManager::GetInstance().IsMultiScreenCollaboration()) {
34         ScreenSessionManager::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DISPLAY_ON,
35             EventStatus::BEGIN, PowerStateChangeReason::STATE_CHANGE_REASON_INIT);
36         ScreenSessionManager::GetInstance().BlockScreenOnByCV();
37     }
38 }
39 
SetDisplayState(DisplayState state)40 bool SessionDisplayPowerController::SetDisplayState(DisplayState state)
41 {
42     TLOGI(WmsLogTag::DMS, "[UL_POWER]state:%{public}u", state);
43     switch (state) {
44         case DisplayState::ON: {
45             SetDisplayStateToOn(state);
46             break;
47         }
48         case DisplayState::OFF: {
49             DisplayState lastState = displayState_;
50             displayState_ = state;
51             if (!ScreenSessionManager::GetInstance().IsMultiScreenCollaboration()) {
52                 {
53                     std::lock_guard<std::mutex> notifyLock(notifyMutex_);
54                     canCancelSuspendNotify_ = false;
55                     if (needCancelNotify_) {
56                         TLOGI(WmsLogTag::DMS, "[UL_POWER]"
57                             "SetDisplayState to OFF is canceled successfully before notify");
58                         needCancelNotify_ = false;
59                         displayState_ = lastState;
60                         ScreenSessionManager::GetInstance().NotifyDisplayStateChanged(DISPLAY_ID_INVALID,
61                             DisplayState::UNKNOWN);
62                         return false;
63                     }
64                     ScreenSessionManager::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DISPLAY_OFF,
65                         EventStatus::BEGIN, PowerStateChangeReason::STATE_CHANGE_REASON_INIT);
66                 }
67                 WaitScreenOffNotify(state);
68 
69                 if (canceledSuspend_) {
70                     TLOGI(WmsLogTag::DMS, "[UL_POWER]SetDisplayState to OFF is canceled successfully after notify");
71                     canceledSuspend_ = false;
72                     displayState_ = lastState;
73                     ScreenSessionManager::GetInstance().NotifyDisplayStateChanged(DISPLAY_ID_INVALID,
74                         DisplayState::UNKNOWN);
75                     return false;
76                 }
77             }
78             break;
79         }
80         default: {
81             TLOGW(WmsLogTag::DMS, "[UL_POWER]unknown DisplayState!");
82             return false;
83         }
84     }
85     ScreenSessionManager::GetInstance().NotifyDisplayStateChanged(DISPLAY_ID_INVALID, state);
86     return true;
87 }
88 
WaitScreenOffNotify(DisplayState & state)89 void SessionDisplayPowerController::WaitScreenOffNotify(DisplayState& state)
90 {
91     if (!ScreenSessionManager::GetInstance().IsPreBrightAuthFail()) {
92         if (!skipScreenOffBlock_) {
93             ScreenSessionManager::GetInstance().BlockScreenOffByCV();
94         }
95         skipScreenOffBlock_ = false;
96         if (ScreenSessionManager::GetInstance().IsScreenLockSuspend()) {
97             state = DisplayState::ON_SUSPEND;
98         }
99     }
100 }
101 
102 
GetDisplayState(DisplayId displayId)103 DisplayState SessionDisplayPowerController::GetDisplayState(DisplayId displayId)
104 {
105     return displayState_;
106 }
107 
NotifyDisplayEvent(DisplayEvent event)108 void SessionDisplayPowerController::NotifyDisplayEvent(DisplayEvent event)
109 {
110     TLOGI(WmsLogTag::DMS, "[UL_POWER]DisplayEvent:%{public}u", event);
111     if (event == DisplayEvent::UNLOCK) {
112         std::map<DisplayId, sptr<DisplayInfo>> emptyMap;
113         displayStateChangeListener_(DISPLAY_ID_INVALID, nullptr, emptyMap, DisplayStateChangeType::BEFORE_UNLOCK);
114         ScreenSessionManager::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DESKTOP_READY,
115             EventStatus::BEGIN, PowerStateChangeReason::STATE_CHANGE_REASON_INIT);
116         return;
117     }
118 }
119 }
120 }