• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
HandleSetDisplayStateOff(DisplayState & state)40 bool SessionDisplayPowerController::HandleSetDisplayStateOff(DisplayState& state)
41 {
42     DisplayState lastState = displayState_;
43     displayState_ = state;
44     if (!ScreenSessionManager::GetInstance().IsMultiScreenCollaboration()) {
45         {
46             std::lock_guard<std::mutex> notifyLock(notifyMutex_);
47             canCancelSuspendNotify_ = false;
48             if (needCancelNotify_) {
49                 TLOGI(WmsLogTag::DMS, "[UL_POWER]SetDisplayState to OFF is canceled successfully before notify");
50                 needCancelNotify_ = false;
51                 displayState_ = lastState;
52                 ScreenSessionManager::GetInstance().NotifyDisplayStateChanged(DISPLAY_ID_INVALID,
53                     DisplayState::UNKNOWN);
54                 return true;
55             }
56             DisplayPowerEvent displayPowerEvent = state == DisplayState::OFF ?
57                 DisplayPowerEvent::DISPLAY_OFF : DisplayPowerEvent::DISPLAY_DOZE;
58             ScreenSessionManager::GetInstance().NotifyDisplayPowerEvent(displayPowerEvent,
59                 EventStatus::BEGIN, PowerStateChangeReason::STATE_CHANGE_REASON_INIT);
60         }
61         if (isSuspendBegin_) {
62             WaitScreenOffNotify(state);
63         }
64         isSuspendBegin_ = false;
65         if (canceledSuspend_) {
66             TLOGI(WmsLogTag::DMS, "[UL_POWER]SetDisplayState to OFF is canceled successfully after notify");
67             canceledSuspend_ = false;
68             displayState_ = lastState;
69             ScreenSessionManager::GetInstance().NotifyDisplayStateChanged(DISPLAY_ID_INVALID,
70                 DisplayState::UNKNOWN);
71             return true;
72         }
73     }
74     return false;
75 }
76 
SetDisplayState(DisplayState state)77 bool SessionDisplayPowerController::SetDisplayState(DisplayState state)
78 {
79     TLOGI(WmsLogTag::DMS, "[UL_POWER]state:%{public}u", state);
80     switch (state) {
81         case DisplayState::ON: {
82             SetDisplayStateToOn(state);
83             break;
84         }
85         case DisplayState::DOZE:
86         case DisplayState::OFF: {
87             if (HandleSetDisplayStateOff(state)) {
88                 return false;
89             }
90             break;
91         }
92         case DisplayState::DOZE_SUSPEND: {
93             ScreenSessionManager::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DISPLAY_DOZE_SUSPEND,
94                 EventStatus::BEGIN, PowerStateChangeReason::STATE_CHANGE_REASON_INIT);
95             break;
96         }
97         default: {
98             TLOGW(WmsLogTag::DMS, "[UL_POWER]unknown DisplayState!");
99             return false;
100         }
101     }
102     ScreenSessionManager::GetInstance().NotifyDisplayStateChanged(DISPLAY_ID_INVALID, state);
103     return true;
104 }
105 
WaitScreenOffNotify(DisplayState & state)106 void SessionDisplayPowerController::WaitScreenOffNotify(DisplayState& state)
107 {
108     if (!ScreenSessionManager::GetInstance().IsPreBrightAuthFail()) {
109         if (!skipScreenOffBlock_) {
110             ScreenSessionManager::GetInstance().BlockScreenOffByCV();
111         }
112         skipScreenOffBlock_ = false;
113         if (ScreenSessionManager::GetInstance().IsScreenLockSuspend()) {
114             state = DisplayState::ON_SUSPEND;
115         }
116     }
117 }
118 
119 
GetDisplayState(DisplayId displayId)120 DisplayState SessionDisplayPowerController::GetDisplayState(DisplayId displayId)
121 {
122     return displayState_;
123 }
124 
NotifyDisplayEvent(DisplayEvent event)125 void SessionDisplayPowerController::NotifyDisplayEvent(DisplayEvent event)
126 {
127     TLOGI(WmsLogTag::DMS, "[UL_POWER]DisplayEvent:%{public}u", event);
128     if (event == DisplayEvent::UNLOCK) {
129         std::map<DisplayId, sptr<DisplayInfo>> emptyMap;
130         displayStateChangeListener_(DISPLAY_ID_INVALID, nullptr, emptyMap, DisplayStateChangeType::BEFORE_UNLOCK);
131         ScreenSessionManager::GetInstance().NotifyDisplayPowerEvent(DisplayPowerEvent::DESKTOP_READY,
132             EventStatus::BEGIN, PowerStateChangeReason::STATE_CHANGE_REASON_INIT);
133         return;
134     }
135 }
136 }
137 }