blob: 4a711ca80b3d4ad6717359e98646c87b023be894 [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>
19#include <android/hardware/power/Mode.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000020#include <powermanager/PowerHalWrapper.h>
Lais Andradeb59a9b52020-05-07 17:23:42 +010021#include <utils/Log.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000022
Lais Andradeb59a9b52020-05-07 17:23:42 +010023using namespace android::hardware::power;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000024
25namespace android {
26
Lais Andradeb59a9b52020-05-07 17:23:42 +010027namespace power {
28
Lais Andrade4d51f6c2020-03-25 10:58:31 +000029// -------------------------------------------------------------------------------------------------
30
Lais Andradeb59a9b52020-05-07 17:23:42 +010031inline HalResult toHalResult(const binder::Status& result) {
Lais Andrade14e97b72020-07-14 12:27:44 +000032 if (result.isOk()) {
33 return HalResult::SUCCESSFUL;
34 }
35 ALOGE("Power HAL request failed: %s", result.toString8().c_str());
36 return HalResult::FAILED;
Lais Andradeb59a9b52020-05-07 17:23:42 +010037}
38
39template <typename T>
40inline HalResult toHalResult(const hardware::Return<T>& result) {
Lais Andrade14e97b72020-07-14 12:27:44 +000041 if (result.isOk()) {
42 return HalResult::SUCCESSFUL;
43 }
44 ALOGE("Power HAL request failed: %s", result.description().c_str());
45 return HalResult::FAILED;
Lais Andradeb59a9b52020-05-07 17:23:42 +010046}
47
48// -------------------------------------------------------------------------------------------------
49
50HalResult EmptyHalWrapper::setBoost(Boost boost, int32_t durationMs) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000051 ALOGV("Skipped setBoost %s with duration %dms because Power HAL not available",
Lais Andradeb59a9b52020-05-07 17:23:42 +010052 toString(boost).c_str(), durationMs);
53 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000054}
55
Lais Andradeb59a9b52020-05-07 17:23:42 +010056HalResult EmptyHalWrapper::setMode(Mode mode, bool enabled) {
57 ALOGV("Skipped setMode %s to %s because Power HAL not available", toString(mode).c_str(),
58 enabled ? "true" : "false");
59 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000060}
61
62// -------------------------------------------------------------------------------------------------
63
Lais Andradeb59a9b52020-05-07 17:23:42 +010064HalResult HidlHalWrapperV1_0::setBoost(Boost boost, int32_t durationMs) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000065 if (boost == Boost::INTERACTION) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010066 return sendPowerHint(V1_0::PowerHint::INTERACTION, durationMs);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000067 } else {
Lais Andradeb59a9b52020-05-07 17:23:42 +010068 ALOGV("Skipped setBoost %s because Power HAL AIDL not available", toString(boost).c_str());
69 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000070 }
71}
72
Lais Andradeb59a9b52020-05-07 17:23:42 +010073HalResult HidlHalWrapperV1_0::setMode(Mode mode, bool enabled) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000074 uint32_t data = enabled ? 1 : 0;
75 switch (mode) {
76 case Mode::LAUNCH:
Lais Andradeb59a9b52020-05-07 17:23:42 +010077 return sendPowerHint(V1_0::PowerHint::LAUNCH, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000078 case Mode::LOW_POWER:
Lais Andradeb59a9b52020-05-07 17:23:42 +010079 return sendPowerHint(V1_0::PowerHint::LOW_POWER, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000080 case Mode::SUSTAINED_PERFORMANCE:
Lais Andradeb59a9b52020-05-07 17:23:42 +010081 return sendPowerHint(V1_0::PowerHint::SUSTAINED_PERFORMANCE, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000082 case Mode::VR:
Lais Andradeb59a9b52020-05-07 17:23:42 +010083 return sendPowerHint(V1_0::PowerHint::VR_MODE, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000084 case Mode::INTERACTIVE:
85 return setInteractive(enabled);
86 case Mode::DOUBLE_TAP_TO_WAKE:
Lais Andradeb59a9b52020-05-07 17:23:42 +010087 return setFeature(V1_0::Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, enabled);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000088 default:
89 ALOGV("Skipped setMode %s because Power HAL AIDL not available",
Lais Andradeb59a9b52020-05-07 17:23:42 +010090 toString(mode).c_str());
91 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000092 }
93}
94
Lais Andradeb59a9b52020-05-07 17:23:42 +010095HalResult HidlHalWrapperV1_0::sendPowerHint(V1_0::PowerHint hintId, uint32_t data) {
96 return toHalResult(mHandleV1_0->powerHint(hintId, data));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000097}
98
Lais Andradeb59a9b52020-05-07 17:23:42 +010099HalResult HidlHalWrapperV1_0::setInteractive(bool enabled) {
100 return toHalResult(mHandleV1_0->setInteractive(enabled));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000101}
102
Lais Andradeb59a9b52020-05-07 17:23:42 +0100103HalResult HidlHalWrapperV1_0::setFeature(V1_0::Feature feature, bool enabled) {
104 return toHalResult(mHandleV1_0->setFeature(feature, enabled));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000105}
106
107// -------------------------------------------------------------------------------------------------
108
Lais Andradeb59a9b52020-05-07 17:23:42 +0100109HalResult HidlHalWrapperV1_1::sendPowerHint(V1_0::PowerHint hintId, uint32_t data) {
110 return toHalResult(mHandleV1_1->powerHintAsync(hintId, data));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000111}
112
113// -------------------------------------------------------------------------------------------------
114
Lais Andradeb59a9b52020-05-07 17:23:42 +0100115HalResult AidlHalWrapper::setBoost(Boost boost, int32_t durationMs) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000116 std::unique_lock<std::mutex> lock(mBoostMutex);
117 // Quick return if boost is not supported by HAL
118 if (boost > Boost::DISPLAY_UPDATE_IMMINENT ||
Lais Andradeb59a9b52020-05-07 17:23:42 +0100119 mBoostSupportedArray[static_cast<int32_t>(boost)] == HalSupport::OFF) {
120 ALOGV("Skipped setBoost %s because Power HAL doesn't support it", toString(boost).c_str());
121 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000122 }
123
Lais Andradeb59a9b52020-05-07 17:23:42 +0100124 if (mBoostSupportedArray[static_cast<int32_t>(boost)] == HalSupport::UNKNOWN) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000125 bool isSupported = false;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100126 auto isSupportedRet = mHandle->isBoostSupported(boost, &isSupported);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000127 if (!isSupportedRet.isOk()) {
Lais Andrade14e97b72020-07-14 12:27:44 +0000128 ALOGE("Skipped setBoost %s because check support failed with: %s",
129 toString(boost).c_str(), isSupportedRet.toString8().c_str());
Lais Andradeb59a9b52020-05-07 17:23:42 +0100130 return HalResult::FAILED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000131 }
132
Lais Andradeb59a9b52020-05-07 17:23:42 +0100133 mBoostSupportedArray[static_cast<int32_t>(boost)] =
134 isSupported ? HalSupport::ON : HalSupport::OFF;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000135 if (!isSupported) {
136 ALOGV("Skipped setBoost %s because Power HAL doesn't support it",
Lais Andradeb59a9b52020-05-07 17:23:42 +0100137 toString(boost).c_str());
138 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000139 }
140 }
141 lock.unlock();
142
Lais Andradeb59a9b52020-05-07 17:23:42 +0100143 return toHalResult(mHandle->setBoost(boost, durationMs));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000144}
145
Lais Andradeb59a9b52020-05-07 17:23:42 +0100146HalResult AidlHalWrapper::setMode(Mode mode, bool enabled) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000147 std::unique_lock<std::mutex> lock(mModeMutex);
148 // Quick return if mode is not supported by HAL
149 if (mode > Mode::DISPLAY_INACTIVE ||
Lais Andradeb59a9b52020-05-07 17:23:42 +0100150 mModeSupportedArray[static_cast<int32_t>(mode)] == HalSupport::OFF) {
151 ALOGV("Skipped setMode %s because Power HAL doesn't support it", toString(mode).c_str());
152 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000153 }
154
Lais Andradeb59a9b52020-05-07 17:23:42 +0100155 if (mModeSupportedArray[static_cast<int32_t>(mode)] == HalSupport::UNKNOWN) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000156 bool isSupported = false;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100157 auto isSupportedRet = mHandle->isModeSupported(mode, &isSupported);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000158 if (!isSupportedRet.isOk()) {
Lais Andrade14e97b72020-07-14 12:27:44 +0000159 ALOGE("Skipped setMode %s because check support failed with: %s",
160 toString(mode).c_str(), isSupportedRet.toString8().c_str());
Lais Andradeb59a9b52020-05-07 17:23:42 +0100161 return HalResult::FAILED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000162 }
163
Lais Andradeb59a9b52020-05-07 17:23:42 +0100164 mModeSupportedArray[static_cast<int32_t>(mode)] =
165 isSupported ? HalSupport::ON : HalSupport::OFF;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000166 if (!isSupported) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100167 ALOGV("Skipped setMode %s because Power HAL doesn't support it",
168 toString(mode).c_str());
169 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000170 }
171 }
172 lock.unlock();
173
Lais Andradeb59a9b52020-05-07 17:23:42 +0100174 return toHalResult(mHandle->setMode(mode, enabled));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000175}
176
177// -------------------------------------------------------------------------------------------------
178
Lais Andradeb59a9b52020-05-07 17:23:42 +0100179} // namespace power
180
181} // namespace android