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 LOG_TAG "HalWrapper"
18 #include <aidl/android/hardware/power/Boost.h>
19 #include <aidl/android/hardware/power/IPowerHintSession.h>
20 #include <aidl/android/hardware/power/Mode.h>
21 #include <powermanager/HalResult.h>
22 #include <powermanager/PowerHalWrapper.h>
23 #include <utils/Log.h>
24
25 using namespace android::hardware::power;
26 namespace Aidl = aidl::android::hardware::power;
27
28 namespace android {
29
30 namespace power {
31
32 // -------------------------------------------------------------------------------------------------
33
setBoost(Aidl::Boost boost,int32_t durationMs)34 HalResult<void> EmptyHalWrapper::setBoost(Aidl::Boost boost, int32_t durationMs) {
35 ALOGV("Skipped setBoost %s with duration %dms because %s", toString(boost).c_str(), durationMs,
36 getUnsupportedMessage());
37 return HalResult<void>::unsupported();
38 }
39
setMode(Aidl::Mode mode,bool enabled)40 HalResult<void> EmptyHalWrapper::setMode(Aidl::Mode mode, bool enabled) {
41 ALOGV("Skipped setMode %s to %s because %s", toString(mode).c_str(), enabled ? "true" : "false",
42 getUnsupportedMessage());
43 return HalResult<void>::unsupported();
44 }
45
createHintSession(int32_t,int32_t,const std::vector<int32_t> & threadIds,int64_t)46 HalResult<std::shared_ptr<PowerHintSessionWrapper>> EmptyHalWrapper::createHintSession(
47 int32_t, int32_t, const std::vector<int32_t>& threadIds, int64_t) {
48 ALOGV("Skipped createHintSession(task num=%zu) because %s", threadIds.size(),
49 getUnsupportedMessage());
50 return HalResult<std::shared_ptr<PowerHintSessionWrapper>>::unsupported();
51 }
52
createHintSessionWithConfig(int32_t,int32_t,const std::vector<int32_t> & threadIds,int64_t,Aidl::SessionTag,Aidl::SessionConfig *)53 HalResult<std::shared_ptr<PowerHintSessionWrapper>> EmptyHalWrapper::createHintSessionWithConfig(
54 int32_t, int32_t, const std::vector<int32_t>& threadIds, int64_t, Aidl::SessionTag,
55 Aidl::SessionConfig*) {
56 ALOGV("Skipped createHintSessionWithConfig(task num=%zu) because %s", threadIds.size(),
57 getUnsupportedMessage());
58 return HalResult<std::shared_ptr<PowerHintSessionWrapper>>::unsupported();
59 }
60
getHintSessionPreferredRate()61 HalResult<int64_t> EmptyHalWrapper::getHintSessionPreferredRate() {
62 ALOGV("Skipped getHintSessionPreferredRate because %s", getUnsupportedMessage());
63 return HalResult<int64_t>::unsupported();
64 }
65
getSessionChannel(int,int)66 HalResult<Aidl::ChannelConfig> EmptyHalWrapper::getSessionChannel(int, int) {
67 ALOGV("Skipped getSessionChannel because %s", getUnsupportedMessage());
68 return HalResult<Aidl::ChannelConfig>::unsupported();
69 }
70
closeSessionChannel(int,int)71 HalResult<void> EmptyHalWrapper::closeSessionChannel(int, int) {
72 ALOGV("Skipped closeSessionChannel because %s", getUnsupportedMessage());
73 return HalResult<void>::unsupported();
74 }
75
getUnsupportedMessage()76 const char* EmptyHalWrapper::getUnsupportedMessage() {
77 return "Power HAL is not supported";
78 }
79
80 // -------------------------------------------------------------------------------------------------
81
setBoost(Aidl::Boost boost,int32_t durationMs)82 HalResult<void> HidlHalWrapperV1_0::setBoost(Aidl::Boost boost, int32_t durationMs) {
83 if (boost == Aidl::Boost::INTERACTION) {
84 return sendPowerHint(V1_3::PowerHint::INTERACTION, durationMs);
85 } else {
86 return EmptyHalWrapper::setBoost(boost, durationMs);
87 }
88 }
89
setMode(Aidl::Mode mode,bool enabled)90 HalResult<void> HidlHalWrapperV1_0::setMode(Aidl::Mode mode, bool enabled) {
91 uint32_t data = enabled ? 1 : 0;
92 switch (mode) {
93 case Aidl::Mode::LAUNCH:
94 return sendPowerHint(V1_3::PowerHint::LAUNCH, data);
95 case Aidl::Mode::LOW_POWER:
96 return sendPowerHint(V1_3::PowerHint::LOW_POWER, data);
97 case Aidl::Mode::SUSTAINED_PERFORMANCE:
98 return sendPowerHint(V1_3::PowerHint::SUSTAINED_PERFORMANCE, data);
99 case Aidl::Mode::VR:
100 return sendPowerHint(V1_3::PowerHint::VR_MODE, data);
101 case Aidl::Mode::INTERACTIVE:
102 return setInteractive(enabled);
103 case Aidl::Mode::DOUBLE_TAP_TO_WAKE:
104 return setFeature(V1_0::Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, enabled);
105 default:
106 return EmptyHalWrapper::setMode(mode, enabled);
107 }
108 }
109
sendPowerHint(V1_3::PowerHint hintId,uint32_t data)110 HalResult<void> HidlHalWrapperV1_0::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
111 auto ret = mHandleV1_0->powerHint(static_cast<V1_0::PowerHint>(hintId), data);
112 return HalResult<void>::fromReturn(ret);
113 }
114
setInteractive(bool enabled)115 HalResult<void> HidlHalWrapperV1_0::setInteractive(bool enabled) {
116 auto ret = mHandleV1_0->setInteractive(enabled);
117 return HalResult<void>::fromReturn(ret);
118 }
119
setFeature(V1_0::Feature feature,bool enabled)120 HalResult<void> HidlHalWrapperV1_0::setFeature(V1_0::Feature feature, bool enabled) {
121 auto ret = mHandleV1_0->setFeature(feature, enabled);
122 return HalResult<void>::fromReturn(ret);
123 }
124
getUnsupportedMessage()125 const char* HidlHalWrapperV1_0::getUnsupportedMessage() {
126 return "Power HAL AIDL is not supported";
127 }
128
129 // -------------------------------------------------------------------------------------------------
130
sendPowerHint(V1_3::PowerHint hintId,uint32_t data)131 HalResult<void> HidlHalWrapperV1_1::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
132 auto handle = static_cast<V1_1::IPower*>(mHandleV1_0.get());
133 auto ret = handle->powerHintAsync(static_cast<V1_0::PowerHint>(hintId), data);
134 return HalResult<void>::fromReturn(ret);
135 }
136
137 // -------------------------------------------------------------------------------------------------
138
sendPowerHint(V1_3::PowerHint hintId,uint32_t data)139 HalResult<void> HidlHalWrapperV1_2::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
140 auto handle = static_cast<V1_2::IPower*>(mHandleV1_0.get());
141 auto ret = handle->powerHintAsync_1_2(static_cast<V1_2::PowerHint>(hintId), data);
142 return HalResult<void>::fromReturn(ret);
143 }
144
setBoost(Aidl::Boost boost,int32_t durationMs)145 HalResult<void> HidlHalWrapperV1_2::setBoost(Aidl::Boost boost, int32_t durationMs) {
146 switch (boost) {
147 case Aidl::Boost::CAMERA_SHOT:
148 return sendPowerHint(V1_3::PowerHint::CAMERA_SHOT, durationMs);
149 case Aidl::Boost::CAMERA_LAUNCH:
150 return sendPowerHint(V1_3::PowerHint::CAMERA_LAUNCH, durationMs);
151 default:
152 return HidlHalWrapperV1_1::setBoost(boost, durationMs);
153 }
154 }
155
setMode(Aidl::Mode mode,bool enabled)156 HalResult<void> HidlHalWrapperV1_2::setMode(Aidl::Mode mode, bool enabled) {
157 uint32_t data = enabled ? 1 : 0;
158 switch (mode) {
159 case Aidl::Mode::CAMERA_STREAMING_SECURE:
160 case Aidl::Mode::CAMERA_STREAMING_LOW:
161 case Aidl::Mode::CAMERA_STREAMING_MID:
162 case Aidl::Mode::CAMERA_STREAMING_HIGH:
163 return sendPowerHint(V1_3::PowerHint::CAMERA_STREAMING, data);
164 case Aidl::Mode::AUDIO_STREAMING_LOW_LATENCY:
165 return sendPowerHint(V1_3::PowerHint::AUDIO_LOW_LATENCY, data);
166 default:
167 return HidlHalWrapperV1_1::setMode(mode, enabled);
168 }
169 }
170
171 // -------------------------------------------------------------------------------------------------
172
setMode(Aidl::Mode mode,bool enabled)173 HalResult<void> HidlHalWrapperV1_3::setMode(Aidl::Mode mode, bool enabled) {
174 uint32_t data = enabled ? 1 : 0;
175 if (mode == Aidl::Mode::EXPENSIVE_RENDERING) {
176 return sendPowerHint(V1_3::PowerHint::EXPENSIVE_RENDERING, data);
177 }
178 return HidlHalWrapperV1_2::setMode(mode, enabled);
179 }
180
sendPowerHint(V1_3::PowerHint hintId,uint32_t data)181 HalResult<void> HidlHalWrapperV1_3::sendPowerHint(V1_3::PowerHint hintId, uint32_t data) {
182 auto handle = static_cast<V1_3::IPower*>(mHandleV1_0.get());
183 auto ret = handle->powerHintAsync_1_3(hintId, data);
184 return HalResult<void>::fromReturn(ret);
185 }
186
187 // -------------------------------------------------------------------------------------------------
188
setBoost(Aidl::Boost boost,int32_t durationMs)189 HalResult<void> AidlHalWrapper::setBoost(Aidl::Boost boost, int32_t durationMs) {
190 std::unique_lock<std::mutex> lock(mBoostMutex);
191 size_t idx = static_cast<size_t>(boost);
192
193 // Quick return if boost is not supported by HAL
194 if (idx >= mBoostSupportedArray.size() || mBoostSupportedArray[idx] == HalSupport::OFF) {
195 ALOGV("Skipped setBoost %s because %s", toString(boost).c_str(), getUnsupportedMessage());
196 return HalResult<void>::unsupported();
197 }
198
199 if (mBoostSupportedArray[idx] == HalSupport::UNKNOWN) {
200 bool isSupported = false;
201 auto isSupportedRet = mHandle->isBoostSupported(boost, &isSupported);
202 if (!isSupportedRet.isOk()) {
203 ALOGE("Skipped setBoost %s because check support failed with: %s",
204 toString(boost).c_str(), isSupportedRet.getDescription().c_str());
205 // return HalResult::FAILED;
206 return HalResult<void>::fromStatus(isSupportedRet);
207 }
208
209 mBoostSupportedArray[idx] = isSupported ? HalSupport::ON : HalSupport::OFF;
210 if (!isSupported) {
211 ALOGV("Skipped setBoost %s because %s", toString(boost).c_str(),
212 getUnsupportedMessage());
213 return HalResult<void>::unsupported();
214 }
215 }
216 lock.unlock();
217
218 return HalResult<void>::fromStatus(mHandle->setBoost(boost, durationMs));
219 }
220
setMode(Aidl::Mode mode,bool enabled)221 HalResult<void> AidlHalWrapper::setMode(Aidl::Mode mode, bool enabled) {
222 std::unique_lock<std::mutex> lock(mModeMutex);
223 size_t idx = static_cast<size_t>(mode);
224
225 // Quick return if mode is not supported by HAL
226 if (idx >= mModeSupportedArray.size() || mModeSupportedArray[idx] == HalSupport::OFF) {
227 ALOGV("Skipped setMode %s because %s", toString(mode).c_str(), getUnsupportedMessage());
228 return HalResult<void>::unsupported();
229 }
230
231 if (mModeSupportedArray[idx] == HalSupport::UNKNOWN) {
232 bool isSupported = false;
233 auto isSupportedRet = mHandle->isModeSupported(mode, &isSupported);
234 if (!isSupportedRet.isOk()) {
235 return HalResult<void>::failed(isSupportedRet.getDescription());
236 }
237
238 mModeSupportedArray[idx] = isSupported ? HalSupport::ON : HalSupport::OFF;
239 if (!isSupported) {
240 ALOGV("Skipped setMode %s because %s", toString(mode).c_str(), getUnsupportedMessage());
241 return HalResult<void>::unsupported();
242 }
243 }
244 lock.unlock();
245
246 return HalResult<void>::fromStatus(mHandle->setMode(mode, enabled));
247 }
248
createHintSession(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos)249 HalResult<std::shared_ptr<PowerHintSessionWrapper>> AidlHalWrapper::createHintSession(
250 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
251 std::shared_ptr<Aidl::IPowerHintSession> appSession;
252 return HalResult<std::shared_ptr<PowerHintSessionWrapper>>::
253 fromStatus(mHandle->createHintSession(tgid, uid, threadIds, durationNanos, &appSession),
254 std::make_shared<PowerHintSessionWrapper>(std::move(appSession)));
255 }
256
createHintSessionWithConfig(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos,Aidl::SessionTag tag,Aidl::SessionConfig * config)257 HalResult<std::shared_ptr<PowerHintSessionWrapper>> AidlHalWrapper::createHintSessionWithConfig(
258 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
259 Aidl::SessionTag tag, Aidl::SessionConfig* config) {
260 std::shared_ptr<Aidl::IPowerHintSession> appSession;
261 return HalResult<std::shared_ptr<PowerHintSessionWrapper>>::
262 fromStatus(mHandle->createHintSessionWithConfig(tgid, uid, threadIds, durationNanos,
263 tag, config, &appSession),
264 std::make_shared<PowerHintSessionWrapper>(std::move(appSession)));
265 }
266
getHintSessionPreferredRate()267 HalResult<int64_t> AidlHalWrapper::getHintSessionPreferredRate() {
268 int64_t rate = -1;
269 auto result = mHandle->getHintSessionPreferredRate(&rate);
270 return HalResult<int64_t>::fromStatus(result, rate);
271 }
272
getSessionChannel(int tgid,int uid)273 HalResult<Aidl::ChannelConfig> AidlHalWrapper::getSessionChannel(int tgid, int uid) {
274 Aidl::ChannelConfig config;
275 auto result = mHandle->getSessionChannel(tgid, uid, &config);
276 return HalResult<Aidl::ChannelConfig>::fromStatus(result, std::move(config));
277 }
278
closeSessionChannel(int tgid,int uid)279 HalResult<void> AidlHalWrapper::closeSessionChannel(int tgid, int uid) {
280 return HalResult<void>::fromStatus(mHandle->closeSessionChannel(tgid, uid));
281 }
282
getUnsupportedMessage()283 const char* AidlHalWrapper::getUnsupportedMessage() {
284 return "Power HAL doesn't support it";
285 }
286
287 // -------------------------------------------------------------------------------------------------
288
289 } // namespace power
290
291 } // namespace android
292