1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
18 #define LOG_TAG "android.hardware.power-service.pixel-libperfmgr"
19
20 #include "Power.h"
21
22 #include <mutex>
23
24 #include <android-base/file.h>
25 #include <android-base/logging.h>
26 #include <android-base/properties.h>
27 #include <android-base/stringprintf.h>
28 #include <android-base/strings.h>
29
30 #include <utils/Log.h>
31 #include <utils/Trace.h>
32
33 #include "disp-power/DisplayLowPower.h"
34
35 namespace aidl {
36 namespace google {
37 namespace hardware {
38 namespace power {
39 namespace impl {
40 namespace pixel {
41
42 constexpr char kPowerHalStateProp[] = "vendor.powerhal.state";
43 constexpr char kPowerHalAudioProp[] = "vendor.powerhal.audio";
44 constexpr char kPowerHalRenderingProp[] = "vendor.powerhal.rendering";
45
Power(std::shared_ptr<HintManager> hm,std::shared_ptr<DisplayLowPower> dlpw)46 Power::Power(std::shared_ptr<HintManager> hm, std::shared_ptr<DisplayLowPower> dlpw)
47 : mHintManager(hm),
48 mDisplayLowPower(dlpw),
49 mInteractionHandler(nullptr),
50 mVRModeOn(false),
51 mSustainedPerfModeOn(false) {
52 mInteractionHandler = std::make_unique<InteractionHandler>(mHintManager);
53 mInteractionHandler->Init();
54
55 std::string state = ::android::base::GetProperty(kPowerHalStateProp, "");
56 if (state == "SUSTAINED_PERFORMANCE") {
57 ALOGI("Initialize with SUSTAINED_PERFORMANCE on");
58 mHintManager->DoHint("SUSTAINED_PERFORMANCE");
59 mSustainedPerfModeOn = true;
60 } else if (state == "VR") {
61 ALOGI("Initialize with VR on");
62 mHintManager->DoHint(state);
63 mVRModeOn = true;
64 } else if (state == "VR_SUSTAINED_PERFORMANCE") {
65 ALOGI("Initialize with SUSTAINED_PERFORMANCE and VR on");
66 mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE");
67 mSustainedPerfModeOn = true;
68 mVRModeOn = true;
69 } else {
70 ALOGI("Initialize PowerHAL");
71 }
72
73 state = ::android::base::GetProperty(kPowerHalAudioProp, "");
74 if (state == "AUDIO_STREAMING_LOW_LATENCY") {
75 ALOGI("Initialize with AUDIO_LOW_LATENCY on");
76 mHintManager->DoHint(state);
77 }
78
79 state = ::android::base::GetProperty(kPowerHalRenderingProp, "");
80 if (state == "EXPENSIVE_RENDERING") {
81 ALOGI("Initialize with EXPENSIVE_RENDERING on");
82 mHintManager->DoHint("EXPENSIVE_RENDERING");
83 }
84
85 // Now start to take powerhint
86 ALOGI("PowerHAL ready to process hints");
87 }
88
setMode(Mode type,bool enabled)89 ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) {
90 LOG(DEBUG) << "Power setMode: " << toString(type) << " to: " << enabled;
91 ATRACE_INT(toString(type).c_str(), enabled);
92 switch (type) {
93 case Mode::LOW_POWER:
94 mDisplayLowPower->SetDisplayLowPower(enabled);
95 if (enabled) {
96 mHintManager->DoHint(toString(type));
97 } else {
98 mHintManager->EndHint(toString(type));
99 }
100 break;
101 case Mode::SUSTAINED_PERFORMANCE:
102 if (enabled && !mSustainedPerfModeOn) {
103 if (!mVRModeOn) { // Sustained mode only.
104 mHintManager->DoHint("SUSTAINED_PERFORMANCE");
105 } else { // Sustained + VR mode.
106 mHintManager->EndHint("VR");
107 mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE");
108 }
109 mSustainedPerfModeOn = true;
110 } else if (!enabled && mSustainedPerfModeOn) {
111 mHintManager->EndHint("VR_SUSTAINED_PERFORMANCE");
112 mHintManager->EndHint("SUSTAINED_PERFORMANCE");
113 if (mVRModeOn) { // Switch back to VR Mode.
114 mHintManager->DoHint("VR");
115 }
116 mSustainedPerfModeOn = false;
117 }
118 break;
119 case Mode::VR:
120 if (enabled && !mVRModeOn) {
121 if (!mSustainedPerfModeOn) { // VR mode only.
122 mHintManager->DoHint("VR");
123 } else { // Sustained + VR mode.
124 mHintManager->EndHint("SUSTAINED_PERFORMANCE");
125 mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE");
126 }
127 mVRModeOn = true;
128 } else if (!enabled && mVRModeOn) {
129 mHintManager->EndHint("VR_SUSTAINED_PERFORMANCE");
130 mHintManager->EndHint("VR");
131 if (mSustainedPerfModeOn) { // Switch back to sustained Mode.
132 mHintManager->DoHint("SUSTAINED_PERFORMANCE");
133 }
134 mVRModeOn = false;
135 }
136 break;
137 case Mode::LAUNCH:
138 if (mVRModeOn || mSustainedPerfModeOn) {
139 break;
140 }
141 [[fallthrough]];
142 case Mode::DOUBLE_TAP_TO_WAKE:
143 [[fallthrough]];
144 case Mode::FIXED_PERFORMANCE:
145 [[fallthrough]];
146 case Mode::EXPENSIVE_RENDERING:
147 [[fallthrough]];
148 case Mode::INTERACTIVE:
149 [[fallthrough]];
150 case Mode::DEVICE_IDLE:
151 [[fallthrough]];
152 case Mode::DISPLAY_INACTIVE:
153 [[fallthrough]];
154 case Mode::AUDIO_STREAMING_LOW_LATENCY:
155 [[fallthrough]];
156 case Mode::CAMERA_STREAMING_SECURE:
157 [[fallthrough]];
158 case Mode::CAMERA_STREAMING_LOW:
159 [[fallthrough]];
160 case Mode::CAMERA_STREAMING_MID:
161 [[fallthrough]];
162 case Mode::CAMERA_STREAMING_HIGH:
163 [[fallthrough]];
164 default:
165 if (enabled) {
166 mHintManager->DoHint(toString(type));
167 } else {
168 mHintManager->EndHint(toString(type));
169 }
170 break;
171 }
172
173 return ndk::ScopedAStatus::ok();
174 }
175
isModeSupported(Mode type,bool * _aidl_return)176 ndk::ScopedAStatus Power::isModeSupported(Mode type, bool *_aidl_return) {
177 bool supported = mHintManager->IsHintSupported(toString(type));
178 // LOW_POWER handled insides PowerHAL specifically
179 if (type == Mode::LOW_POWER) {
180 supported = true;
181 }
182 LOG(INFO) << "Power mode " << toString(type) << " isModeSupported: " << supported;
183 *_aidl_return = supported;
184 return ndk::ScopedAStatus::ok();
185 }
186
setBoost(Boost type,int32_t durationMs)187 ndk::ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) {
188 LOG(DEBUG) << "Power setBoost: " << toString(type) << " duration: " << durationMs;
189 ATRACE_INT(toString(type).c_str(), durationMs);
190 switch (type) {
191 case Boost::INTERACTION:
192 if (mVRModeOn || mSustainedPerfModeOn) {
193 break;
194 }
195 mInteractionHandler->Acquire(durationMs);
196 break;
197 case Boost::DISPLAY_UPDATE_IMMINENT:
198 [[fallthrough]];
199 case Boost::ML_ACC:
200 [[fallthrough]];
201 case Boost::AUDIO_LAUNCH:
202 [[fallthrough]];
203 case Boost::CAMERA_LAUNCH:
204 [[fallthrough]];
205 case Boost::CAMERA_SHOT:
206 [[fallthrough]];
207 default:
208 if (mVRModeOn || mSustainedPerfModeOn) {
209 break;
210 }
211 if (durationMs > 0) {
212 mHintManager->DoHint(toString(type), std::chrono::milliseconds(durationMs));
213 } else if (durationMs == 0) {
214 mHintManager->DoHint(toString(type));
215 } else {
216 mHintManager->EndHint(toString(type));
217 }
218 break;
219 }
220
221 return ndk::ScopedAStatus::ok();
222 }
223
isBoostSupported(Boost type,bool * _aidl_return)224 ndk::ScopedAStatus Power::isBoostSupported(Boost type, bool *_aidl_return) {
225 bool supported = mHintManager->IsHintSupported(toString(type));
226 LOG(INFO) << "Power boost " << toString(type) << " isBoostSupported: " << supported;
227 *_aidl_return = supported;
228 return ndk::ScopedAStatus::ok();
229 }
230
boolToString(bool b)231 constexpr const char *boolToString(bool b) {
232 return b ? "true" : "false";
233 }
234
dump(int fd,const char **,uint32_t)235 binder_status_t Power::dump(int fd, const char **, uint32_t) {
236 std::string buf(::android::base::StringPrintf(
237 "HintManager Running: %s\n"
238 "VRMode: %s\n"
239 "SustainedPerformanceMode: %s\n",
240 boolToString(mHintManager->IsRunning()), boolToString(mVRModeOn),
241 boolToString(mSustainedPerfModeOn)));
242 // Dump nodes through libperfmgr
243 mHintManager->DumpToFd(fd);
244 if (!::android::base::WriteStringToFd(buf, fd)) {
245 PLOG(ERROR) << "Failed to dump state to fd";
246 }
247 fsync(fd);
248 return STATUS_OK;
249 }
250
251 } // namespace pixel
252 } // namespace impl
253 } // namespace power
254 } // namespace hardware
255 } // namespace google
256 } // namespace aidl
257