From 4d51f6ce2a9028a68b3092aab6a528cb69b0c56c Mon Sep 17 00:00:00 2001 From: Lais Andrade Date: Wed, 25 Mar 2020 10:58:31 +0000 Subject: Create wrappers for Power HAL. Create wrappers over existing HIDL Power HALs v1.0, v1.1, and AIDL Power HAL. All wrappers expose the same API that should be used by the PowerManagerService. Bug: 150878220 Test: atest powermanager_test Change-Id: I5edba6d51d3e18c5137c87b65bc9efa6c70ba7e8 --- services/powermanager/PowerHalWrapper.cpp | 172 ++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 services/powermanager/PowerHalWrapper.cpp (limited to 'services/powermanager/PowerHalWrapper.cpp') diff --git a/services/powermanager/PowerHalWrapper.cpp b/services/powermanager/PowerHalWrapper.cpp new file mode 100644 index 0000000000..d959a2cc96 --- /dev/null +++ b/services/powermanager/PowerHalWrapper.cpp @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "PowerHalWrapper" +#include + +#include +#include + +#include + +using android::hardware::power::Boost; +using android::hardware::power::Mode; +using android::hardware::power::V1_0::Feature; +using android::hardware::power::V1_0::PowerHint; + +namespace android { + +// ------------------------------------------------------------------------------------------------- + +PowerHalResult EmptyPowerHalWrapper::setBoost(Boost boost, int32_t durationMs) { + ALOGV("Skipped setBoost %s with duration %dms because Power HAL not available", + toString(boost).c_str(), durationMs); + return PowerHalResult::UNSUPPORTED; +} + +PowerHalResult EmptyPowerHalWrapper::setMode(Mode mode, bool enabled) { + ALOGV("Skipped setMode %s to %s because Power HAL not available", + toString(mode).c_str(), enabled ? "true" : "false"); + return PowerHalResult::UNSUPPORTED; +} + +// ------------------------------------------------------------------------------------------------- + +PowerHalResult HidlPowerHalWrapperV1_0::setBoost(Boost boost, int32_t durationMs) { + if (boost == Boost::INTERACTION) { + return sendPowerHint(PowerHint::INTERACTION, durationMs); + } else { + ALOGV("Skipped setBoost %s because Power HAL AIDL not available", + toString(boost).c_str()); + return PowerHalResult::UNSUPPORTED; + } +} + +PowerHalResult HidlPowerHalWrapperV1_0::setMode(Mode mode, bool enabled) { + uint32_t data = enabled ? 1 : 0; + switch (mode) { + case Mode::LAUNCH: + return sendPowerHint(PowerHint::LAUNCH, data); + case Mode::LOW_POWER: + return sendPowerHint(PowerHint::LOW_POWER, data); + case Mode::SUSTAINED_PERFORMANCE: + return sendPowerHint(PowerHint::SUSTAINED_PERFORMANCE, data); + case Mode::VR: + return sendPowerHint(PowerHint::VR_MODE, data); + case Mode::INTERACTIVE: + return setInteractive(enabled); + case Mode::DOUBLE_TAP_TO_WAKE: + return setFeature(Feature::POWER_FEATURE_DOUBLE_TAP_TO_WAKE, enabled); + default: + ALOGV("Skipped setMode %s because Power HAL AIDL not available", + toString(mode).c_str()); + return PowerHalResult::UNSUPPORTED; + } +} + +PowerHalResult HidlPowerHalWrapperV1_0::sendPowerHint(PowerHint hintId, uint32_t data) { + auto ret = handleV1_0->powerHint(hintId, data); + return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED; +} + +PowerHalResult HidlPowerHalWrapperV1_0::setInteractive(bool enabled) { + auto ret = handleV1_0->setInteractive(enabled); + return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED; +} + +PowerHalResult HidlPowerHalWrapperV1_0::setFeature(Feature feature, bool enabled) { + auto ret = handleV1_0->setFeature(feature, enabled); + return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED; +} + +// ------------------------------------------------------------------------------------------------- + +PowerHalResult HidlPowerHalWrapperV1_1::sendPowerHint(PowerHint hintId, uint32_t data) { + auto ret = handleV1_1->powerHintAsync(hintId, data); + return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED; +} + +// ------------------------------------------------------------------------------------------------- + +PowerHalResult AidlPowerHalWrapper::setBoost(Boost boost, int32_t durationMs) { + std::unique_lock lock(mBoostMutex); + // Quick return if boost is not supported by HAL + if (boost > Boost::DISPLAY_UPDATE_IMMINENT || + boostSupportedArray[static_cast(boost)] == PowerHalSupport::OFF) { + ALOGV("Skipped setBoost %s because Power HAL doesn't support it", + toString(boost).c_str()); + return PowerHalResult::UNSUPPORTED; + } + + if (boostSupportedArray[static_cast(boost)] == PowerHalSupport::UNKNOWN) { + bool isSupported = false; + auto isSupportedRet = handle->isBoostSupported(boost, &isSupported); + if (!isSupportedRet.isOk()) { + ALOGV("Skipped setBoost %s because Power HAL is not available to check support", + toString(boost).c_str()); + return PowerHalResult::FAILED; + } + + boostSupportedArray[static_cast(boost)] = + isSupported ? PowerHalSupport::ON : PowerHalSupport::OFF; + if (!isSupported) { + ALOGV("Skipped setBoost %s because Power HAL doesn't support it", + toString(boost).c_str()); + return PowerHalResult::UNSUPPORTED; + } + } + lock.unlock(); + + auto ret = handle->setBoost(boost, durationMs); + return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED; +} + +PowerHalResult AidlPowerHalWrapper::setMode(Mode mode, bool enabled) { + std::unique_lock lock(mModeMutex); + // Quick return if mode is not supported by HAL + if (mode > Mode::DISPLAY_INACTIVE || + modeSupportedArray[static_cast(mode)] == PowerHalSupport::OFF) { + ALOGV("Skipped setMode %s because Power HAL doesn't support it", + toString(mode).c_str()); + return PowerHalResult::UNSUPPORTED; + } + + if (modeSupportedArray[static_cast(mode)] == PowerHalSupport::UNKNOWN) { + bool isSupported = false; + auto isSupportedRet = handle->isModeSupported(mode, &isSupported); + if (!isSupportedRet.isOk()) { + ALOGV("Skipped setMode %s because Power HAL is not available to check support", + toString(mode).c_str()); + return PowerHalResult::FAILED; + } + + modeSupportedArray[static_cast(mode)] = + isSupported ? PowerHalSupport::ON : PowerHalSupport::OFF; + if (!isSupported) { + ALOGV("Skipped setMode %s because Power HAL doesn't support it", + toString(mode).c_str()); + return PowerHalResult::UNSUPPORTED; + } + } + lock.unlock(); + + auto ret = handle->setMode(mode, enabled); + return ret.isOk() ? PowerHalResult::SUCCESSFUL : PowerHalResult::FAILED; +} + +// ------------------------------------------------------------------------------------------------- + +}; // namespace android -- cgit v1.2.3-59-g8ed1b