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