blob: 95f862370bcda340d0e6363e21606862e850d2ff [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) {
32 return result.isOk() ? HalResult::SUCCESSFUL : HalResult::FAILED;
33}
34
35template <typename T>
36inline HalResult toHalResult(const hardware::Return<T>& result) {
37 return result.isOk() ? HalResult::SUCCESSFUL : HalResult::FAILED;
38}
39
40// -------------------------------------------------------------------------------------------------
41
42HalResult EmptyHalWrapper::setBoost(Boost boost, int32_t durationMs) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000043 ALOGV("Skipped setBoost %s with duration %dms because Power HAL not available",
Lais Andradeb59a9b52020-05-07 17:23:42 +010044 toString(boost).c_str(), durationMs);
45 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000046}
47
Lais Andradeb59a9b52020-05-07 17:23:42 +010048HalResult EmptyHalWrapper::setMode(Mode mode, bool enabled) {
49 ALOGV("Skipped setMode %s to %s because Power HAL not available", toString(mode).c_str(),
50 enabled ? "true" : "false");
51 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000052}
53
54// -------------------------------------------------------------------------------------------------
55
Lais Andradeb59a9b52020-05-07 17:23:42 +010056HalResult HidlHalWrapperV1_0::setBoost(Boost boost, int32_t durationMs) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000057 if (boost == Boost::INTERACTION) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010058 return sendPowerHint(V1_0::PowerHint::INTERACTION, durationMs);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000059 } else {
Lais Andradeb59a9b52020-05-07 17:23:42 +010060 ALOGV("Skipped setBoost %s because Power HAL AIDL not available", toString(boost).c_str());
61 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000062 }
63}
64
Lais Andradeb59a9b52020-05-07 17:23:42 +010065HalResult HidlHalWrapperV1_0::setMode(Mode mode, bool enabled) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000066 uint32_t data = enabled ? 1 : 0;
67 switch (mode) {
68 case Mode::LAUNCH:
Lais Andradeb59a9b52020-05-07 17:23:42 +010069 return sendPowerHint(V1_0::PowerHint::LAUNCH, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000070 case Mode::LOW_POWER:
Lais Andradeb59a9b52020-05-07 17:23:42 +010071 return sendPowerHint(V1_0::PowerHint::LOW_POWER, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000072 case Mode::SUSTAINED_PERFORMANCE:
Lais Andradeb59a9b52020-05-07 17:23:42 +010073 return sendPowerHint(V1_0::PowerHint::SUSTAINED_PERFORMANCE, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000074 case Mode::VR:
Lais Andradeb59a9b52020-05-07 17:23:42 +010075 return sendPowerHint(V1_0::PowerHint::VR_MODE, data);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000076 case Mode::INTERACTIVE:
77 return setInteractive(enabled);
78 case Mode::DOUBLE_TAP_TO_WAKE:
Lais Andradeb59a9b52020-05-07 17:23:42 +010079 return setFeature(V1_0::Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, enabled);
Lais Andrade4d51f6c2020-03-25 10:58:31 +000080 default:
81 ALOGV("Skipped setMode %s because Power HAL AIDL not available",
Lais Andradeb59a9b52020-05-07 17:23:42 +010082 toString(mode).c_str());
83 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000084 }
85}
86
Lais Andradeb59a9b52020-05-07 17:23:42 +010087HalResult HidlHalWrapperV1_0::sendPowerHint(V1_0::PowerHint hintId, uint32_t data) {
88 return toHalResult(mHandleV1_0->powerHint(hintId, data));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000089}
90
Lais Andradeb59a9b52020-05-07 17:23:42 +010091HalResult HidlHalWrapperV1_0::setInteractive(bool enabled) {
92 return toHalResult(mHandleV1_0->setInteractive(enabled));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000093}
94
Lais Andradeb59a9b52020-05-07 17:23:42 +010095HalResult HidlHalWrapperV1_0::setFeature(V1_0::Feature feature, bool enabled) {
96 return toHalResult(mHandleV1_0->setFeature(feature, enabled));
Lais Andrade4d51f6c2020-03-25 10:58:31 +000097}
98
99// -------------------------------------------------------------------------------------------------
100
Lais Andradeb59a9b52020-05-07 17:23:42 +0100101HalResult HidlHalWrapperV1_1::sendPowerHint(V1_0::PowerHint hintId, uint32_t data) {
102 return toHalResult(mHandleV1_1->powerHintAsync(hintId, data));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000103}
104
105// -------------------------------------------------------------------------------------------------
106
Lais Andradeb59a9b52020-05-07 17:23:42 +0100107HalResult AidlHalWrapper::setBoost(Boost boost, int32_t durationMs) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000108 std::unique_lock<std::mutex> lock(mBoostMutex);
109 // Quick return if boost is not supported by HAL
110 if (boost > Boost::DISPLAY_UPDATE_IMMINENT ||
Lais Andradeb59a9b52020-05-07 17:23:42 +0100111 mBoostSupportedArray[static_cast<int32_t>(boost)] == HalSupport::OFF) {
112 ALOGV("Skipped setBoost %s because Power HAL doesn't support it", toString(boost).c_str());
113 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000114 }
115
Lais Andradeb59a9b52020-05-07 17:23:42 +0100116 if (mBoostSupportedArray[static_cast<int32_t>(boost)] == HalSupport::UNKNOWN) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000117 bool isSupported = false;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100118 auto isSupportedRet = mHandle->isBoostSupported(boost, &isSupported);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000119 if (!isSupportedRet.isOk()) {
Lais Andrade159eb6a2020-06-24 15:11:05 +0000120 ALOGV("Skipped setBoost %s because Power HAL is not available to check support",
Lais Andradeb59a9b52020-05-07 17:23:42 +0100121 toString(boost).c_str());
122 return HalResult::FAILED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000123 }
124
Lais Andradeb59a9b52020-05-07 17:23:42 +0100125 mBoostSupportedArray[static_cast<int32_t>(boost)] =
126 isSupported ? HalSupport::ON : HalSupport::OFF;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000127 if (!isSupported) {
128 ALOGV("Skipped setBoost %s because Power HAL doesn't support it",
Lais Andradeb59a9b52020-05-07 17:23:42 +0100129 toString(boost).c_str());
130 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000131 }
132 }
133 lock.unlock();
134
Lais Andradeb59a9b52020-05-07 17:23:42 +0100135 return toHalResult(mHandle->setBoost(boost, durationMs));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000136}
137
Lais Andradeb59a9b52020-05-07 17:23:42 +0100138HalResult AidlHalWrapper::setMode(Mode mode, bool enabled) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000139 std::unique_lock<std::mutex> lock(mModeMutex);
140 // Quick return if mode is not supported by HAL
141 if (mode > Mode::DISPLAY_INACTIVE ||
Lais Andradeb59a9b52020-05-07 17:23:42 +0100142 mModeSupportedArray[static_cast<int32_t>(mode)] == HalSupport::OFF) {
143 ALOGV("Skipped setMode %s because Power HAL doesn't support it", toString(mode).c_str());
144 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000145 }
146
Lais Andradeb59a9b52020-05-07 17:23:42 +0100147 if (mModeSupportedArray[static_cast<int32_t>(mode)] == HalSupport::UNKNOWN) {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000148 bool isSupported = false;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100149 auto isSupportedRet = mHandle->isModeSupported(mode, &isSupported);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000150 if (!isSupportedRet.isOk()) {
Lais Andrade159eb6a2020-06-24 15:11:05 +0000151 ALOGV("Skipped setMode %s because Power HAL is not available to check support",
Lais Andradeb59a9b52020-05-07 17:23:42 +0100152 toString(mode).c_str());
153 return HalResult::FAILED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000154 }
155
Lais Andradeb59a9b52020-05-07 17:23:42 +0100156 mModeSupportedArray[static_cast<int32_t>(mode)] =
157 isSupported ? HalSupport::ON : HalSupport::OFF;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000158 if (!isSupported) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100159 ALOGV("Skipped setMode %s because Power HAL doesn't support it",
160 toString(mode).c_str());
161 return HalResult::UNSUPPORTED;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000162 }
163 }
164 lock.unlock();
165
Lais Andradeb59a9b52020-05-07 17:23:42 +0100166 return toHalResult(mHandle->setMode(mode, enabled));
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000167}
168
169// -------------------------------------------------------------------------------------------------
170
Lais Andradeb59a9b52020-05-07 17:23:42 +0100171} // namespace power
172
173} // namespace android