• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "screen_action.h"
17 
18 #include <hisysevent.h>
19 #include <ipc_skeleton.h>
20 
21 #include "dm_common.h"
22 #include "display_manager.h"
23 #include "display_log.h"
24 #include "power_state_machine_info.h"
25 #include "screen_manager.h"
26 
27 namespace OHOS {
28 namespace DisplayPowerMgr {
ScreenAction(uint32_t displayId)29 ScreenAction::ScreenAction(uint32_t displayId) : displayId_(displayId)
30 {}
31 
GetDefaultDisplayId()32 uint32_t ScreenAction::GetDefaultDisplayId()
33 {
34     std::string identity = IPCSkeleton::ResetCallingIdentity();
35     uint64_t defaultId = Rosen::DisplayManager::GetInstance().GetDefaultDisplayId();
36     IPCSkeleton::SetCallingIdentity(identity);
37     return static_cast<uint32_t>(defaultId);
38 }
39 
GetAllDisplayId()40 std::vector<uint32_t> ScreenAction::GetAllDisplayId()
41 {
42     std::string identity = IPCSkeleton::ResetCallingIdentity();
43     std::vector<uint64_t> allIds = Rosen::DisplayManager::GetInstance().GetAllDisplayIds();
44     IPCSkeleton::SetCallingIdentity(identity);
45     std::vector<uint32_t> displayIds;
46     if (allIds.empty()) {
47         displayIds.push_back(DEFAULT_DISPLAY_ID);
48         return displayIds;
49     }
50     std::transform(allIds.begin(), allIds.end(), back_inserter(displayIds), [](uint64_t id) {
51         return static_cast<uint32_t>(id);
52     });
53     return displayIds;
54 }
55 
GetDisplayId()56 uint32_t ScreenAction::GetDisplayId()
57 {
58     return displayId_;
59 }
60 
GetDisplayState()61 DisplayState ScreenAction::GetDisplayState()
62 {
63     DisplayState state = DisplayState::DISPLAY_UNKNOWN;
64     Rosen::ScreenPowerState powerState = Rosen::ScreenManager::GetInstance().GetScreenPower(displayId_);
65     DISPLAY_HILOGI(FEAT_STATE, "ScreenPowerState=%{public}d", static_cast<uint32_t>(powerState));
66     switch (powerState) {
67         case Rosen::ScreenPowerState::POWER_ON:
68             state = DisplayState::DISPLAY_ON;
69             break;
70         case Rosen::ScreenPowerState::POWER_STAND_BY:
71             state = DisplayState::DISPLAY_DIM;
72             break;
73         case Rosen::ScreenPowerState::POWER_SUSPEND:
74             state = DisplayState::DISPLAY_SUSPEND;
75             break;
76         case Rosen::ScreenPowerState::POWER_OFF:
77             state = DisplayState::DISPLAY_OFF;
78             break;
79         default:
80             break;
81     }
82     DISPLAY_HILOGI(FEAT_STATE, "state=%{public}u displayId=%{public}u", static_cast<uint32_t>(state), displayId_);
83     return state;
84 }
85 
SetDisplayState(DisplayState state,const std::function<void (DisplayState)> & callback)86 bool ScreenAction::SetDisplayState(DisplayState state, const std::function<void(DisplayState)>& callback)
87 {
88     DISPLAY_HILOGI(FEAT_STATE, "displayId=%{public}u, state=%{public}u", displayId_, static_cast<uint32_t>(state));
89     Rosen::DisplayState rds = Rosen::DisplayState::UNKNOWN;
90     switch (state) {
91         case DisplayState::DISPLAY_ON:
92             rds = Rosen::DisplayState::ON;
93             break;
94         case DisplayState::DISPLAY_OFF:
95             rds = Rosen::DisplayState::OFF;
96             break;
97         default:
98             break;
99     }
100     std::string identity = IPCSkeleton::ResetCallingIdentity();
101     bool ret = Rosen::DisplayManager::GetInstance().SetDisplayState(rds,
102         [callback](Rosen::DisplayState rosenState) {
103             DISPLAY_HILOGI(FEAT_STATE, "SetDisplayState Callback:%{public}d", static_cast<uint32_t>(rosenState));
104             DisplayState state;
105             switch (rosenState) {
106                 case Rosen::DisplayState::ON:
107                     state = DisplayState::DISPLAY_ON;
108                     break;
109                 case Rosen::DisplayState::OFF:
110                     state = DisplayState::DISPLAY_OFF;
111                     break;
112                 default:
113                     return;
114             }
115             callback(state);
116     });
117     IPCSkeleton::SetCallingIdentity(identity);
118     // Notify screen state change event to battery statistics
119     HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::DISPLAY, "SCREEN_STATE",
120         HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", static_cast<int32_t>(state));
121     DISPLAY_HILOGI(FEAT_STATE, "SetDisplayState:%{public}d", ret);
122     return ret;
123 }
124 
SetDisplayPower(DisplayState state,uint32_t reason)125 bool ScreenAction::SetDisplayPower(DisplayState state, uint32_t reason)
126 {
127     DISPLAY_HILOGI(FEAT_STATE, "displayId=%{public}u, state=%{public}u, reason=%{public}u",
128                    displayId_, static_cast<uint32_t>(state), reason);
129     Rosen::ScreenPowerState status = Rosen::ScreenPowerState::INVALID_STATE;
130     switch (state) {
131         case DisplayState::DISPLAY_ON:
132             status = Rosen::ScreenPowerState::POWER_ON;
133             break;
134         case DisplayState::DISPLAY_DIM:
135             status = Rosen::ScreenPowerState::POWER_STAND_BY;
136             break;
137         case DisplayState::DISPLAY_SUSPEND:
138             status = Rosen::ScreenPowerState::POWER_SUSPEND;
139             break;
140         case DisplayState::DISPLAY_OFF:
141             status = Rosen::ScreenPowerState::POWER_OFF;
142             break;
143         default:
144             break;
145     }
146 
147     bool ret = false;
148     if (coordinated_ && reason == static_cast<uint32_t>(PowerMgr::StateChangeReason::STATE_CHANGE_REASON_TIMEOUT)) {
149         ret = Rosen::ScreenManager::GetInstance().SetSpecifiedScreenPower(
150             displayId_, status, Rosen::PowerStateChangeReason::STATE_CHANGE_REASON_COLLABORATION);
151     } else {
152         ret = Rosen::ScreenManager::GetInstance().SetScreenPowerForAll(
153             status, Rosen::PowerStateChangeReason::POWER_BUTTON);
154     }
155     DISPLAY_HILOGI(FEAT_STATE, "Set screen power, ret=%{public}d, coordinated=%{public}d", ret, coordinated_);
156     return (state == DisplayState::DISPLAY_DIM || state == DisplayState::DISPLAY_SUSPEND) ? true : ret;
157 }
158 
GetBrightness()159 uint32_t ScreenAction::GetBrightness()
160 {
161     std::lock_guard lock(mutexBrightness_);
162     std::string identity = IPCSkeleton::ResetCallingIdentity();
163     brightness_ = Rosen::DisplayManager::GetInstance().GetScreenBrightness(displayId_);
164     IPCSkeleton::SetCallingIdentity(identity);
165     DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId_, brightness_);
166     return brightness_;
167 }
168 
SetBrightness(uint32_t value)169 bool ScreenAction::SetBrightness(uint32_t value)
170 {
171     DISPLAY_HILOGD(FEAT_BRIGHTNESS, "displayId=%{public}u, brightness=%{public}u", displayId_, value);
172     // Notify screen brightness change event to battery statistics
173     HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::DISPLAY, "BRIGHTNESS_NIT",
174         HiviewDFX::HiSysEvent::EventType::STATISTIC, "BRIGHTNESS", value);
175     std::string identity = IPCSkeleton::ResetCallingIdentity();
176     bool isSucc = Rosen::DisplayManager::GetInstance().SetScreenBrightness(displayId_, value);
177     IPCSkeleton::SetCallingIdentity(identity);
178     std::lock_guard lock(mutexBrightness_);
179     brightness_ = isSucc ? value : brightness_;
180     return isSucc;
181 }
182 
SetCoordinated(bool coordinated)183 void ScreenAction::SetCoordinated(bool coordinated)
184 {
185     coordinated_ = coordinated;
186 }
187 } // namespace DisplayPowerMgr
188 } // namespace OHOS
189