blob: 9e7adf8e5cc42f48de93b68cadf50fca425fd9b5 [file] [log] [blame]
Lais Andrade4d51f6c2020-03-25 10:58:31 +00001/*
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
Lais Andradeb59a9b52020-05-07 17:23:42 +010017#define LOG_TAG "HalWrapper"
Lais Andrade4d51f6c2020-03-25 10:58:31 +000018#include <android/hardware/power/Boost.h>
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080019#include <android/hardware/power/IPowerHintSession.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000020#include <android/hardware/power/Mode.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000021#include <powermanager/PowerHalWrapper.h>
Lais Andradeb59a9b52020-05-07 17:23:42 +010022#include <utils/Log.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000023
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080024#include <cinttypes>
25
Lais Andradeb59a9b52020-05-07 17:23:42 +010026using namespace android::hardware::power;
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080027namespace Aidl = android::hardware::power;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000028
29namespace android {
30
Lais Andradeb59a9b52020-05-07 17:23:42 +010031namespace power {
32
Lais Andrade4d51f6c2020-03-25 10:58:31 +000033// -------------------------------------------------------------------------------------------------
34
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080035inline HalResult<void> toHalResult(const binder::Status& result) {
Lais Andrade14e97b72020-07-14 12:27:44 +000036 if (result.isOk()) {
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080037 return HalResult<void>::ok();
Lais Andrade14e97b72020-07-14 12:27:44 +000038 }
39 ALOGE("Power HAL request failed: %s", result.toString8().c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080040 return HalResult<void>::fromStatus(result);
Lais Andradeb59a9b52020-05-07 17:23:42 +010041}
42
43template <typename T>
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080044template <typename R>
45HalResult<T> HalResult<T>::fromReturn(hardware::Return<R>& ret, T data) {
46 return ret.isOk() ? HalResult<T>::ok(data) : HalResult<T>::failed(ret.description());
47}
48
49template <typename T>
50template <typename R>
51HalResult<T> HalResult<T>::fromReturn(hardware::Return<R>& ret, V1_0::Status status, T data) {
52 return ret.isOk() ? HalResult<T>::fromStatus(status, data)
53 : HalResult<T>::failed(ret.description());
Lais Andradeb59a9b52020-05-07 17:23:42 +010054}
55
56// -------------------------------------------------------------------------------------------------
57
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080058HalResult<void> HalResult<void>::fromStatus(status_t status) {
59 if (status == android::OK) {
60 return HalResult<void>::ok();
61 }
62 return HalResult<void>::failed(statusToString(status));
63}
64
65HalResult<void> HalResult<void>::fromStatus(binder::Status status) {
66 if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) {
67 return HalResult<void>::unsupported();
68 }
69 if (status.isOk()) {
70 return HalResult<void>::ok();
71 }
72 return HalResult<void>::failed(std::string(status.toString8().c_str()));
73}
74
75template <typename R>
76HalResult<void> HalResult<void>::fromReturn(hardware::Return<R>& ret) {
77 return ret.isOk() ? HalResult<void>::ok() : HalResult<void>::failed(ret.description());
78}
79// -------------------------------------------------------------------------------------------------
80
81HalResult<void> EmptyHalWrapper::setBoost(Boost boost, int32_t durationMs) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000082 ALOGV("Skipped setBoost %s with duration %dms because Power HAL not available",
Lais Andradeb59a9b52020-05-07 17:23:42 +010083 toString(boost).c_str(), durationMs);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080084 return HalResult<void>::unsupported();
Lais Andrade4d51f6c2020-03-25 10:58:31 +000085}
86
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080087HalResult<void> EmptyHalWrapper::setMode(Mode mode, bool enabled) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010088 ALOGV("Skipped setMode %s to %s because Power HAL not available", toString(mode).c_str(),
89 enabled ? "true" : "false");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080090 return HalResult<void>::unsupported();
91}
92
93HalResult<sp<Aidl::IPowerHintSession>> EmptyHalWrapper::createHintSession(
94 int32_t, int32_t, const std::vector<int32_t>& threadIds, int64_t) {
95 ALOGV("Skipped createHintSession(task num=%zu) because Power HAL not available",
96 threadIds.size());
97 return HalResult<sp<Aidl::IPowerHintSession>>::unsupported();
98}
99
100HalResult<int64_t> EmptyHalWrapper::getHintSessionPreferredRate() {
101 ALOGV("Skipped getHintSessionPreferredRate because Power HAL not available");
102 return HalResult<int64_t>::unsupported();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000103}
104
105// -------------------------------------------------------------------------------------------------
106
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800107HalResult<void> HidlHalWrapperV1_0::setBoost(Boost boost, int32_t durationMs) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000108 if (boost == Boost::INTERACTION) {
Matt Buckleyc3894a42022-09-01 21:17:15 +0000109 return sendPowerHint(V1_3::PowerHint::INTERACTION, durationMs);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000110 } else {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100111 ALOGV("Skipped setBoost %s because Power HAL AIDL not available", toString(boost).c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800112 return HalResult<void>::unsupported();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000113 }
114}
115
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800116HalResult<void> HidlHalWrapperV1_0::setMode(Mode mode, bool enabled) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000117 uint32_t data = enabled ? 1 : 0;
118 switch (mode) {
119 case Mode::LAUNCH:
Matt Buckleyc3894a42022-09-01 21:17:15 +0000120 return sendPowerHint(V1_3::PowerHint::LAUNCH, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000121 case Mode::LOW_POWER:
Matt Buckleyc3894a42022-09-01 21:17:15 +0000122 return sendPowerHint(V1_3::PowerHint::LOW_POWER, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000123 case Mode::SUSTAINED_PERFORMANCE:
Matt Buckleyc3894a42022-09-01 21:17:15 +0000124 return sendPowerHint(V1_3::PowerHint::SUSTAINED_PERFORMANCE, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000125 case Mode::VR:
Matt Buckleyc3894a42022-09-01 21:17:15 +0000126 return sendPowerHint(V1_3::PowerHint::VR_MODE, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000127 case Mode::INTERACTIVE:
128 return setInteractive(enabled);
129 case Mode::DOUBLE_TAP_TO_WAKE:
Lais Andradeb59a9b52020-05-07 17:23:42 +0100130 return setFeature(V1_0::Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, enabled);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000131 default:
132 ALOGV("Skipped setMode %s because Power HAL AIDL not available",
Lais Andradeb59a9b52020-05-07 17:23:42 +0100133 toString(mode).c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800134 return HalResult<void>::unsupported();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000135 }
136}
137
Matt Buckleyc3894a42022-09-01 21:17:15 +0000138HalResult<void> HidlHalWrapperV1_0::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
139 auto ret = mHandleV1_0->powerHint(static_cast<V1_0::PowerHint>(hintId), data);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800140 return HalResult<void>::fromReturn(ret);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000141}
142
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800143HalResult<void> HidlHalWrapperV1_0::setInteractive(bool enabled) {
144 auto ret = mHandleV1_0->setInteractive(enabled);
145 return HalResult<void>::fromReturn(ret);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000146}
147
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800148HalResult<void> HidlHalWrapperV1_0::setFeature(V1_0::Feature feature, bool enabled) {
149 auto ret = mHandleV1_0->setFeature(feature, enabled);
150 return HalResult<void>::fromReturn(ret);
151}
152
Matt Buckleyc3894a42022-09-01 21:17:15 +0000153HalResult<sp<hardware::power::IPowerHintSession>> HidlHalWrapperV1_0::createHintSession(
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800154 int32_t, int32_t, const std::vector<int32_t>& threadIds, int64_t) {
155 ALOGV("Skipped createHintSession(task num=%zu) because Power HAL not available",
156 threadIds.size());
157 return HalResult<sp<Aidl::IPowerHintSession>>::unsupported();
158}
159
160HalResult<int64_t> HidlHalWrapperV1_0::getHintSessionPreferredRate() {
161 ALOGV("Skipped getHintSessionPreferredRate because Power HAL not available");
162 return HalResult<int64_t>::unsupported();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000163}
164
165// -------------------------------------------------------------------------------------------------
166
Matt Buckleyc3894a42022-09-01 21:17:15 +0000167HalResult<void> HidlHalWrapperV1_1::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
168 auto handle = static_cast<V1_1::IPower*>(mHandleV1_0.get());
169 auto ret = handle->powerHintAsync(static_cast<V1_0::PowerHint>(hintId), data);
170 return HalResult<void>::fromReturn(ret);
171}
172
173// -------------------------------------------------------------------------------------------------
174
175HalResult<void> HidlHalWrapperV1_2::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
176 auto handle = static_cast<V1_2::IPower*>(mHandleV1_0.get());
177 auto ret = handle->powerHintAsync_1_2(static_cast<V1_2::PowerHint>(hintId), data);
178 return HalResult<void>::fromReturn(ret);
179}
180
181HalResult<void> HidlHalWrapperV1_2::setBoost(Boost boost, int32_t durationMs) {
182 switch (boost) {
183 case Boost::CAMERA_SHOT:
184 return sendPowerHint(V1_3::PowerHint::CAMERA_SHOT, durationMs);
185 case Boost::CAMERA_LAUNCH:
186 return sendPowerHint(V1_3::PowerHint::CAMERA_LAUNCH, durationMs);
187 default:
188 return HidlHalWrapperV1_1::setBoost(boost, durationMs);
189 }
190}
191
192HalResult<void> HidlHalWrapperV1_2::setMode(Mode mode, bool enabled) {
193 uint32_t data = enabled ? 1 : 0;
194 switch (mode) {
195 case Mode::CAMERA_STREAMING_SECURE:
196 case Mode::CAMERA_STREAMING_LOW:
197 case Mode::CAMERA_STREAMING_MID:
198 case Mode::CAMERA_STREAMING_HIGH:
199 return sendPowerHint(V1_3::PowerHint::CAMERA_STREAMING, data);
200 case Mode::AUDIO_STREAMING_LOW_LATENCY:
201 return sendPowerHint(V1_3::PowerHint::AUDIO_LOW_LATENCY, data);
202 default:
203 return HidlHalWrapperV1_1::setMode(mode, enabled);
204 }
205}
206
207// -------------------------------------------------------------------------------------------------
208
209HalResult<void> HidlHalWrapperV1_3::setMode(Mode mode, bool enabled) {
210 uint32_t data = enabled ? 1 : 0;
211 if (mode == Mode::EXPENSIVE_RENDERING) {
212 return sendPowerHint(V1_3::PowerHint::EXPENSIVE_RENDERING, data);
213 }
214 return HidlHalWrapperV1_2::setMode(mode, enabled);
215}
216
217HalResult<void> HidlHalWrapperV1_3::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
218 auto handle = static_cast<V1_3::IPower*>(mHandleV1_0.get());
219 auto ret = handle->powerHintAsync_1_3(hintId, data);
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800220 return HalResult<void>::fromReturn(ret);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000221}
222
223// -------------------------------------------------------------------------------------------------
224
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800225HalResult<void> AidlHalWrapper::setBoost(Boost boost, int32_t durationMs) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000226 std::unique_lock<std::mutex> lock(mBoostMutex);
Lais Andrade7b632a52020-12-03 21:07:48 +0000227 size_t idx = static_cast<size_t>(boost);
228
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000229 // Quick return if boost is not supported by HAL
Lais Andrade7b632a52020-12-03 21:07:48 +0000230 if (idx >= mBoostSupportedArray.size() || mBoostSupportedArray[idx] == HalSupport::OFF) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100231 ALOGV("Skipped setBoost %s because Power HAL doesn't support it", toString(boost).c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800232 return HalResult<void>::unsupported();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000233 }
234
Lais Andrade7b632a52020-12-03 21:07:48 +0000235 if (mBoostSupportedArray[idx] == HalSupport::UNKNOWN) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000236 bool isSupported = false;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100237 auto isSupportedRet = mHandle->isBoostSupported(boost, &isSupported);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000238 if (!isSupportedRet.isOk()) {
Lais Andrade14e97b72020-07-14 12:27:44 +0000239 ALOGE("Skipped setBoost %s because check support failed with: %s",
240 toString(boost).c_str(), isSupportedRet.toString8().c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800241 // return HalResult::FAILED;
242 return HalResult<void>::fromStatus(isSupportedRet);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000243 }
244
Lais Andrade7b632a52020-12-03 21:07:48 +0000245 mBoostSupportedArray[idx] = isSupported ? HalSupport::ON : HalSupport::OFF;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000246 if (!isSupported) {
247 ALOGV("Skipped setBoost %s because Power HAL doesn't support it",
Lais Andradeb59a9b52020-05-07 17:23:42 +0100248 toString(boost).c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800249 return HalResult<void>::unsupported();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000250 }
251 }
252 lock.unlock();
253
Lais Andradeb59a9b52020-05-07 17:23:42 +0100254 return toHalResult(mHandle->setBoost(boost, durationMs));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000255}
256
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800257HalResult<void> AidlHalWrapper::setMode(Mode mode, bool enabled) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000258 std::unique_lock<std::mutex> lock(mModeMutex);
Lais Andrade7b632a52020-12-03 21:07:48 +0000259 size_t idx = static_cast<size_t>(mode);
260
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000261 // Quick return if mode is not supported by HAL
Lais Andrade7b632a52020-12-03 21:07:48 +0000262 if (idx >= mModeSupportedArray.size() || mModeSupportedArray[idx] == HalSupport::OFF) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100263 ALOGV("Skipped setMode %s because Power HAL doesn't support it", toString(mode).c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800264 return HalResult<void>::unsupported();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000265 }
266
Lais Andrade7b632a52020-12-03 21:07:48 +0000267 if (mModeSupportedArray[idx] == HalSupport::UNKNOWN) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000268 bool isSupported = false;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100269 auto isSupportedRet = mHandle->isModeSupported(mode, &isSupported);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000270 if (!isSupportedRet.isOk()) {
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800271 return HalResult<void>::failed(isSupportedRet.toString8().c_str());
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000272 }
273
Lais Andrade7b632a52020-12-03 21:07:48 +0000274 mModeSupportedArray[idx] = isSupported ? HalSupport::ON : HalSupport::OFF;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000275 if (!isSupported) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100276 ALOGV("Skipped setMode %s because Power HAL doesn't support it",
277 toString(mode).c_str());
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800278 return HalResult<void>::unsupported();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000279 }
280 }
281 lock.unlock();
282
Lais Andradeb59a9b52020-05-07 17:23:42 +0100283 return toHalResult(mHandle->setMode(mode, enabled));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000284}
285
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800286HalResult<sp<Aidl::IPowerHintSession>> AidlHalWrapper::createHintSession(
287 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
288 sp<IPowerHintSession> appSession;
289 return HalResult<sp<Aidl::IPowerHintSession>>::
290 fromStatus(mHandle->createHintSession(tgid, uid, threadIds, durationNanos, &appSession),
291 appSession);
292}
293
294HalResult<int64_t> AidlHalWrapper::getHintSessionPreferredRate() {
295 int64_t rate = -1;
296 auto result = mHandle->getHintSessionPreferredRate(&rate);
297 return HalResult<int64_t>::fromStatus(result, rate);
298}
299
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000300// -------------------------------------------------------------------------------------------------
301
Lais Andradeb59a9b52020-05-07 17:23:42 +0100302} // namespace power
303
304} // namespace android