Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c0945ca8 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Create wrappers for Power HAL."

parents 245c084a 4d51f6ce
Loading
Loading
Loading
Loading
+128 −0
Original line number Diff line number Diff line
/*
 * 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.
 */

#ifndef ANDROID_POWERHALWRAPPER_H
#define ANDROID_POWERHALWRAPPER_H

#include <android-base/thread_annotations.h>

#include <android/hardware/power/1.1/IPower.h>
#include <android/hardware/power/Boost.h>
#include <android/hardware/power/IPower.h>
#include <android/hardware/power/Mode.h>

using android::hardware::power::Boost;
using android::hardware::power::Mode;
using android::hardware::power::V1_0::Feature;
using android::hardware::power::V1_0::PowerHint;
using IPowerV1_1 = android::hardware::power::V1_1::IPower;
using IPowerV1_0 = android::hardware::power::V1_0::IPower;
using IPowerAidl = android::hardware::power::IPower;

namespace android {

// State of Power HAL support for individual apis.
enum class PowerHalSupport {
    UNKNOWN = 0,
    ON = 1,
    OFF = 2,
};

// State of the Power HAL api call result.
enum class PowerHalResult {
    SUCCESSFUL = 0,
    FAILED = 1,
    UNSUPPORTED = 2,
};

// Wrapper for Power HAL handlers.
class PowerHalWrapper {
public:
    virtual ~PowerHalWrapper() = default;

    virtual PowerHalResult setBoost(Boost boost, int32_t durationMs) = 0;
    virtual PowerHalResult setMode(Mode mode, bool enabled) = 0;
};

// Empty Power HAL wrapper that ignores all api calls.
class EmptyPowerHalWrapper : public PowerHalWrapper {
public:
    EmptyPowerHalWrapper() = default;
    ~EmptyPowerHalWrapper() = default;

    PowerHalResult setBoost(Boost boost, int32_t durationMs) override;
    PowerHalResult setMode(Mode mode, bool enabled) override;
};

// Wrapper for the HIDL Power HAL v1.0.
class HidlPowerHalWrapperV1_0 : public PowerHalWrapper {
public:
    explicit HidlPowerHalWrapperV1_0(sp<IPowerV1_0> powerHal) : handleV1_0(std::move(powerHal)) {}
    virtual ~HidlPowerHalWrapperV1_0() = default;

    PowerHalResult setBoost(Boost boost, int32_t durationMs) override;
    PowerHalResult setMode(Mode mode, bool enabled) override;

protected:
    virtual PowerHalResult sendPowerHint(PowerHint hintId, uint32_t data);

private:
    sp<IPowerV1_0> handleV1_0;
    PowerHalResult setInteractive(bool enabled);
    PowerHalResult setFeature(Feature feature, bool enabled);
};

// Wrapper for the HIDL Power HAL v1.1.
class HidlPowerHalWrapperV1_1 : public HidlPowerHalWrapperV1_0 {
public:
    HidlPowerHalWrapperV1_1(sp<IPowerV1_0> powerHalV1_0, sp<IPowerV1_1> powerHalV1_1)
        : HidlPowerHalWrapperV1_0(powerHalV1_0), handleV1_1(std::move(powerHalV1_1)) {}
    ~HidlPowerHalWrapperV1_1() = default;

protected:
    virtual PowerHalResult sendPowerHint(PowerHint hintId, uint32_t data) override;

private:
    sp<IPowerV1_1> handleV1_1;
};

// Wrapper for the AIDL Power HAL.
class AidlPowerHalWrapper : public PowerHalWrapper {
public:
    explicit AidlPowerHalWrapper(sp<IPowerAidl> powerHal) : handle(std::move(powerHal)) {}
    ~AidlPowerHalWrapper() = default;

    PowerHalResult setBoost(Boost boost, int32_t durationMs) override;
    PowerHalResult setMode(Mode mode, bool enabled) override;

private:
    // Control access to the boost and mode supported arrays.
    std::mutex mBoostMutex;
    std::mutex mModeMutex;
    sp<IPowerAidl> handle;
    // Android framework only sends boost upto DISPLAY_UPDATE_IMMINENT.
    // Need to increase the array size if more boost supported.
    std::array<std::atomic<PowerHalSupport>, static_cast<int32_t>(Boost::DISPLAY_UPDATE_IMMINENT)+1>
        boostSupportedArray GUARDED_BY(mBoostMutex) = {PowerHalSupport::UNKNOWN};
    // Android framework only sends mode upto DISPLAY_INACTIVE.
    // Need to increase the array if more mode supported.
    std::array<std::atomic<PowerHalSupport>, static_cast<int32_t>(Mode::DISPLAY_INACTIVE)+1>
        modeSupportedArray GUARDED_BY(mModeMutex) = {PowerHalSupport::UNKNOWN};
};

}; // namespace android

#endif // ANDROID_POWERHALWRAPPER_H
+7 −21
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ cc_library_shared {
    srcs: [
        "BatterySaverPolicyConfig.cpp",
        "CoolingDevice.cpp",
        "PowerHalWrapper.cpp",
        "PowerSaveState.cpp",
        "Temperature.cpp",
        "WorkSource.cpp",
@@ -19,9 +20,13 @@ cc_library_shared {
    },

    shared_libs: [
        "libutils",
        "libbinder",
        "liblog"
        "libhidlbase",
        "liblog",
        "libutils",
        "android.hardware.power@1.0",
        "android.hardware.power@1.1",
        "android.hardware.power-cpp",
    ],

    cflags: [
@@ -36,22 +41,3 @@ cc_library_shared {
         "include",
    ],
}

cc_test {
    name: "thermalmanager-test",
    srcs: ["IThermalManagerTest.cpp",
          ],
    cflags: [
        "-Wall",
        "-Werror",
        "-Wextra",
    ],
    shared_libs: [
        "libbase",
        "libbinder",
        "libhidlbase",
        "liblog",
        "libpowermanager",
        "libutils",
    ],
}
+172 −0
Original line number Diff line number Diff line
/*
 * 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 <utils/Log.h>

#include <android/hardware/power/Boost.h>
#include <android/hardware/power/Mode.h>

#include <powermanager/PowerHalWrapper.h>

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<std::mutex> lock(mBoostMutex);
    // Quick return if boost is not supported by HAL
    if (boost > Boost::DISPLAY_UPDATE_IMMINENT ||
        boostSupportedArray[static_cast<int32_t>(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<int32_t>(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<int32_t>(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<std::mutex> lock(mModeMutex);
    // Quick return if mode is not supported by HAL
    if (mode > Mode::DISPLAY_INACTIVE ||
        modeSupportedArray[static_cast<int32_t>(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<int32_t>(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<int32_t>(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
+10 −0
Original line number Diff line number Diff line
{
  "presubmit": [
    {
      "name": "powermanager_test"
    },
    {
      "name": "thermalmanager_test"
    }
  ]
}
+61 −0
Original line number Diff line number Diff line
// 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.

cc_test {
    name: "powermanager_test",
    test_suites: ["device-tests"],
    srcs: [
        "PowerHalWrapperAidlTest.cpp",
        "PowerHalWrapperHidlV1_0Test.cpp",
        "PowerHalWrapperHidlV1_1Test.cpp",
    ],
    cflags: [
        "-Wall",
        "-Werror",
        "-Wextra",
    ],
    shared_libs: [
        "libbase",
        "libbinder",
        "libhidlbase",
        "liblog",
        "libpowermanager",
        "libutils",
        "android.hardware.power@1.0",
        "android.hardware.power@1.1",
        "android.hardware.power-cpp",
    ],
    static_libs: [
        "libgmock",
    ],
}

cc_test {
    name: "thermalmanager_test",
    test_suites: ["device-tests"],
    srcs: ["IThermalManagerTest.cpp",],
    cflags: [
        "-Wall",
        "-Werror",
        "-Wextra",
    ],
    shared_libs: [
        "libbase",
        "libhidlbase",
        "liblog",
        "libpowermanager",
        "libbinder",
        "libutils",
    ],
}
Loading