blob: d959a2cc964e74142c42929d1e10627ee812f30b [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
17#define LOG_TAG "PowerHalWrapper"
18#include <utils/Log.h>
19
20#include <android/hardware/power/Boost.h>
21#include <android/hardware/power/Mode.h>
22
23#include <powermanager/PowerHalWrapper.h>
24
25using android::hardware::power::Boost;
26using android::hardware::power::Mode;
27using android::hardware::power::V1_0::Feature;
28using android::hardware::power::V1_0::PowerHint;
29
30namespace android {
31
32// -------------------------------------------------------------------------------------------------
33
34PowerHalResult EmptyPowerHalWrapper::setBoost(Boost boost, int32_t durationMs) {
35 ALOGV("Skipped setBoost %s with duration %dms because Power HAL not available",
36 toString(boost).c_str(), durationMs);
37 return PowerHalResult::UNSUPPORTED;
38}
39
40PowerHalResult EmptyPowerHalWrapper::setMode(Mode mode, bool enabled) {
41 ALOGV("Skipped setMode %s to %s because Power HAL not available",
42 toString(mode).c_str(), enabled ? "true" : "false");
43 return PowerHalResult::UNSUPPORTED;
44}
45
46// -------------------------------------------------------------------------------------------------
47
48PowerHalResult HidlPowerHalWrapperV1_0::setBoost(Boost boost, int32_t durationMs) {
49 if (boost == Boost::INTERACTION) {
50 return sendPowerHint(PowerHint::INTERACTION, durationMs);
51 } else {
52 ALOGV("Skipped setBoost %s because Power HAL AIDL not available",
53 toString(boost).c_str());
54 return PowerHalResult::UNSUPPORTED;
55 }
56}
57
58PowerHalResult HidlPowerHalWrapperV1_0::setMode(Mode mode, bool enabled) {
59 uint32_t data = enabled ? 1 : 0;
60 switch (mode) {
61 case Mode::LAUNCH:
62 return sendPowerHint(PowerHint::LAUNCH, data);
63 case Mode::LOW_POWER:
64 return sendPowerHint(PowerHint::LOW_POWER, data);
65 case Mode::SUSTAINED_PERFORMANCE:
66 return sendPowerHint(PowerHint::SUSTAINED_PERFORMANCE, data);
67 case Mode::VR:
68 return sendPowerHint(PowerHint::VR_MODE, data);
69 case Mode::INTERACTIVE:
70 return setInteractive(enabled);
71 case Mode::DOUBLE_TAP_TO_WAKE:
72 return setFeature(Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, enabled);
73 default:
74 ALOGV("Skipped setMode %s because Power HAL AIDL not available",
75 toString(mode).c_str());
76 return PowerHalResult::UNSUPPORTED;
77 }
78}
79
80PowerHalResult HidlPowerHalWrapperV1_0::sendPowerHint(PowerHint hintId, uint32_t data) {
81 auto ret = handleV1_0->powerHint(hintId, data);
82 return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED;
83}
84
85PowerHalResult HidlPowerHalWrapperV1_0::setInteractive(bool enabled) {
86 auto ret = handleV1_0->setInteractive(enabled);
87 return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED;
88}
89
90PowerHalResult HidlPowerHalWrapperV1_0::setFeature(Feature feature, bool enabled) {
91 auto ret = handleV1_0->setFeature(feature, enabled);
92 return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED;
93}
94
95// -------------------------------------------------------------------------------------------------
96
97PowerHalResult HidlPowerHalWrapperV1_1::sendPowerHint(PowerHint hintId, uint32_t data) {
98 auto ret = handleV1_1->powerHintAsync(hintId, data);
99 return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED;
100}
101
102// -------------------------------------------------------------------------------------------------
103
104PowerHalResult AidlPowerHalWrapper::setBoost(Boost boost, int32_t durationMs) {
105 std::unique_lock<std::mutex> lock(mBoostMutex);
106 // Quick return if boost is not supported by HAL
107 if (boost > Boost::DISPLAY_UPDATE_IMMINENT ||
108 boostSupportedArray[static_cast<int32_t>(boost)] == PowerHalSupport::OFF) {
109 ALOGV("Skipped setBoost %s because Power HAL doesn't support it",
110 toString(boost).c_str());
111 return PowerHalResult::UNSUPPORTED;
112 }
113
114 if (boostSupportedArray[static_cast<int32_t>(boost)] == PowerHalSupport::UNKNOWN) {
115 bool isSupported = false;
116 auto isSupportedRet = handle->isBoostSupported(boost, &isSupported);
117 if (!isSupportedRet.isOk()) {
118 ALOGV("Skipped setBoost %s because Power HAL is not available to check support",
119 toString(boost).c_str());
120 return PowerHalResult::FAILED;
121 }
122
123 boostSupportedArray[static_cast<int32_t>(boost)] =
124 isSupported ? PowerHalSupport::ON : PowerHalSupport::OFF;
125 if (!isSupported) {
126 ALOGV("Skipped setBoost %s because Power HAL doesn't support it",
127 toString(boost).c_str());
128 return PowerHalResult::UNSUPPORTED;
129 }
130 }
131 lock.unlock();
132
133 auto ret = handle->setBoost(boost, durationMs);
134 return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED;
135}
136
137PowerHalResult AidlPowerHalWrapper::setMode(Mode mode, bool enabled) {
138 std::unique_lock<std::mutex> lock(mModeMutex);
139 // Quick return if mode is not supported by HAL
140 if (mode > Mode::DISPLAY_INACTIVE ||
141 modeSupportedArray[static_cast<int32_t>(mode)] == PowerHalSupport::OFF) {
142 ALOGV("Skipped setMode %s because Power HAL doesn't support it",
143 toString(mode).c_str());
144 return PowerHalResult::UNSUPPORTED;
145 }
146
147 if (modeSupportedArray[static_cast<int32_t>(mode)] == PowerHalSupport::UNKNOWN) {
148 bool isSupported = false;
149 auto isSupportedRet = handle->isModeSupported(mode, &isSupported);
150 if (!isSupportedRet.isOk()) {
151 ALOGV("Skipped setMode %s because Power HAL is not available to check support",
152 toString(mode).c_str());
153 return PowerHalResult::FAILED;
154 }
155
156 modeSupportedArray[static_cast<int32_t>(mode)] =
157 isSupported ? PowerHalSupport::ON : PowerHalSupport::OFF;
158 if (!isSupported) {
159 ALOGV("Skipped setMode %s because Power HAL doesn't support it",
160 toString(mode).c_str());
161 return PowerHalResult::UNSUPPORTED;
162 }
163 }
164 lock.unlock();
165
166 auto ret = handle->setMode(mode, enabled);
167 return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED;
168}
169
170// -------------------------------------------------------------------------------------------------
171
172}; // namespace android