/* * 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 #include #include #include #include #include namespace android { namespace power { // State of Power HAL support for individual apis. enum class HalSupport { UNKNOWN = 0, ON = 1, OFF = 2, }; // Result of a call to the Power HAL wrapper, holding data if successful. template class HalResult { public: static HalResult ok(T value) { return HalResult(value); } static HalResult failed(std::string msg) { return HalResult(std::move(msg), /* unsupported= */ false); } static HalResult unsupported() { return HalResult("", /* unsupported= */ true); } static HalResult fromStatus(binder::Status status, T data) { if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { return HalResult::unsupported(); } if (status.isOk()) { return HalResult::ok(data); } return HalResult::failed(std::string(status.toString8().c_str())); } static HalResult fromStatus(hardware::power::V1_0::Status status, T data); template static HalResult fromReturn(hardware::Return& ret, T data); template static HalResult fromReturn(hardware::Return& ret, hardware::power::V1_0::Status status, T data); // This will throw std::bad_optional_access if this result is not ok. const T& value() const { return mValue.value(); } bool isOk() const { return !mUnsupported && mValue.has_value(); } bool isFailed() const { return !mUnsupported && !mValue.has_value(); } bool isUnsupported() const { return mUnsupported; } const char* errorMessage() const { return mErrorMessage.c_str(); } private: std::optional mValue; std::string mErrorMessage; bool mUnsupported; explicit HalResult(T value) : mValue(std::make_optional(value)), mErrorMessage(), mUnsupported(false) {} explicit HalResult(std::string errorMessage, bool unsupported) : mValue(), mErrorMessage(std::move(errorMessage)), mUnsupported(unsupported) {} }; // Empty result of a call to the Power HAL wrapper. template <> class HalResult { public: static HalResult ok() { return HalResult(); } static HalResult failed(std::string msg) { return HalResult(std::move(msg)); } static HalResult unsupported() { return HalResult(/* unsupported= */ true); } static HalResult fromStatus(status_t status); static HalResult fromStatus(binder::Status status); static HalResult fromStatus(hardware::power::V1_0::Status status); template static HalResult fromReturn(hardware::Return& ret); bool isOk() const { return !mUnsupported && !mFailed; } bool isFailed() const { return !mUnsupported && mFailed; } bool isUnsupported() const { return mUnsupported; } const char* errorMessage() const { return mErrorMessage.c_str(); } private: std::string mErrorMessage; bool mFailed; bool mUnsupported; explicit HalResult(bool unsupported = false) : mErrorMessage(), mFailed(false), mUnsupported(unsupported) {} explicit HalResult(std::string errorMessage) : mErrorMessage(std::move(errorMessage)), mFailed(true), mUnsupported(false) {} }; // Wrapper for Power HAL handlers. class HalWrapper { public: virtual ~HalWrapper() = default; virtual HalResult setBoost(hardware::power::Boost boost, int32_t durationMs) = 0; virtual HalResult setMode(hardware::power::Mode mode, bool enabled) = 0; virtual HalResult> createHintSession( int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos) = 0; virtual HalResult getHintSessionPreferredRate() = 0; }; // Empty Power HAL wrapper that ignores all api calls. class EmptyHalWrapper : public HalWrapper { public: EmptyHalWrapper() = default; ~EmptyHalWrapper() = default; virtual HalResult setBoost(hardware::power::Boost boost, int32_t durationMs) override; virtual HalResult setMode(hardware::power::Mode mode, bool enabled) override; virtual HalResult> createHintSession( int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos) override; virtual HalResult getHintSessionPreferredRate() override; }; // Wrapper for the HIDL Power HAL v1.0. class HidlHalWrapperV1_0 : public HalWrapper { public: explicit HidlHalWrapperV1_0(sp Hal) : mHandleV1_0(std::move(Hal)) {} virtual ~HidlHalWrapperV1_0() = default; virtual HalResult setBoost(hardware::power::Boost boost, int32_t durationMs) override; virtual HalResult setMode(hardware::power::Mode mode, bool enabled) override; virtual HalResult> createHintSession( int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos) override; virtual HalResult getHintSessionPreferredRate() override; protected: virtual HalResult sendPowerHint(hardware::power::V1_0::PowerHint hintId, uint32_t data); private: sp mHandleV1_0; HalResult setInteractive(bool enabled); HalResult setFeature(hardware::power::V1_0::Feature feature, bool enabled); }; // Wrapper for the HIDL Power HAL v1.1. class HidlHalWrapperV1_1 : public HidlHalWrapperV1_0 { public: HidlHalWrapperV1_1(sp handleV1_0, sp handleV1_1) : HidlHalWrapperV1_0(std::move(handleV1_0)), mHandleV1_1(std::move(handleV1_1)) {} virtual ~HidlHalWrapperV1_1() = default; protected: virtual HalResult sendPowerHint(hardware::power::V1_0::PowerHint hintId, uint32_t data) override; private: sp mHandleV1_1; }; // Wrapper for the AIDL Power HAL. class AidlHalWrapper : public HalWrapper { public: explicit AidlHalWrapper(sp handle) : mHandle(std::move(handle)) {} virtual ~AidlHalWrapper() = default; virtual HalResult setBoost(hardware::power::Boost boost, int32_t durationMs) override; virtual HalResult setMode(hardware::power::Mode mode, bool enabled) override; virtual HalResult> createHintSession( int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos) override; virtual HalResult getHintSessionPreferredRate() override; private: // Control access to the boost and mode supported arrays. std::mutex mBoostMutex; std::mutex mModeMutex; sp mHandle; // Android framework only sends boost upto DISPLAY_UPDATE_IMMINENT. // Need to increase the array size if more boost supported. std::array, static_cast(hardware::power::Boost::DISPLAY_UPDATE_IMMINENT) + 1> mBoostSupportedArray GUARDED_BY(mBoostMutex) = {HalSupport::UNKNOWN}; // Android framework only sends mode upto DISPLAY_INACTIVE. // Need to increase the array if more mode supported. std::array, static_cast(hardware::power::Mode::DISPLAY_INACTIVE) + 1> mModeSupportedArray GUARDED_BY(mModeMutex) = {HalSupport::UNKNOWN}; }; }; // namespace power }; // namespace android #endif // ANDROID_POWERHALWRAPPER_H