From 6c18e6da2aa9758eb7c9a639f44a15a45723f65b Mon Sep 17 00:00:00 2001 From: Matt Buckley Date: Wed, 7 Feb 2024 23:39:50 +0000 Subject: Update PowerHAL wrapper support checking behavior - Updates support checks to check status for UNKNOWN_TRANSACTION - Adds PowerHintSessionWrapper class to check support on session methods - Ensures that wrapper methods check the HAL version number for support - Adds macros to cache returned wrapper call support status Bug: 324255931 Test: atest libpowermanager_test Test: atest libsurfaceflinger_unittest:PowerAdvisorTest Change-Id: I4b329e6b55c53198bb064a34e792be6336e66e27 --- include/powermanager/HalResult.h | 165 +++++++++++++++++++++++ include/powermanager/PowerHalController.h | 17 +-- include/powermanager/PowerHalLoader.h | 4 + include/powermanager/PowerHalWrapper.h | 180 ++++--------------------- include/powermanager/PowerHintSessionWrapper.h | 54 ++++++++ 5 files changed, 257 insertions(+), 163 deletions(-) create mode 100644 include/powermanager/HalResult.h create mode 100644 include/powermanager/PowerHintSessionWrapper.h (limited to 'include/powermanager') diff --git a/include/powermanager/HalResult.h b/include/powermanager/HalResult.h new file mode 100644 index 0000000000..7fe3822be0 --- /dev/null +++ b/include/powermanager/HalResult.h @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2024 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. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace android::power { + +static bool checkUnsupported(const ndk::ScopedAStatus& ndkStatus) { + return ndkStatus.getExceptionCode() == EX_UNSUPPORTED_OPERATION || + ndkStatus.getStatus() == STATUS_UNKNOWN_TRANSACTION; +} + +static bool checkUnsupported(const binder::Status& status) { + return status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION || + status.transactionError() == UNKNOWN_TRANSACTION; +} + +// Result of a call to the Power HAL wrapper, holding data if successful. +template +class HalResult { +public: + static HalResult ok(T&& value) { return HalResult(std::forward(value)); } + static HalResult ok(T& value) { return HalResult::ok(T{value}); } + static HalResult failed(std::string msg) { return HalResult(msg, /* unsupported= */ false); } + static HalResult unsupported() { return HalResult("", /* unsupported= */ true); } + + static HalResult fromStatus(const binder::Status& status, T&& data) { + if (checkUnsupported(status)) { + return HalResult::unsupported(); + } + if (status.isOk()) { + return HalResult::ok(std::forward(data)); + } + return HalResult::failed(std::string(status.toString8().c_str())); + } + + static HalResult fromStatus(const binder::Status& status, T& data) { + return HalResult::fromStatus(status, T{data}); + } + + static HalResult fromStatus(const ndk::ScopedAStatus& ndkStatus, T&& data) { + if (checkUnsupported(ndkStatus)) { + return HalResult::unsupported(); + } + if (ndkStatus.isOk()) { + return HalResult::ok(std::forward(data)); + } + return HalResult::failed(std::string(ndkStatus.getDescription())); + } + + static HalResult fromStatus(const ndk::ScopedAStatus& ndkStatus, T& data) { + return HalResult::fromStatus(ndkStatus, T{data}); + } + + template + static HalResult fromReturn(hardware::Return& ret, T&& data) { + return ret.isOk() ? HalResult::ok(std::forward(data)) + : HalResult::failed(ret.description()); + } + + template + static HalResult fromReturn(hardware::Return& ret, T& data) { + return HalResult::fromReturn(ret, T{data}); + } + + template + static HalResult fromReturn(hardware::Return& ret, hardware::power::V1_0::Status status, + T&& data) { + return ret.isOk() ? HalResult::fromStatus(status, std::forward(data)) + : HalResult::failed(ret.description()); + } + + template + static HalResult fromReturn(hardware::Return& ret, hardware::power::V1_0::Status status, + T& data) { + return HalResult::fromReturn(ret, 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::move(value)}, mErrorMessage(), mUnsupported(false) {} + explicit HalResult(std::string errorMessage, bool unsupported) + : mValue(), mErrorMessage(std::move(errorMessage)), mUnsupported(unsupported) {} +}; + +// Empty result +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(const binder::Status& status) { + if (checkUnsupported(status)) { + return HalResult::unsupported(); + } + if (status.isOk()) { + return HalResult::ok(); + } + return HalResult::failed(std::string(status.toString8().c_str())); + } + + static HalResult fromStatus(const ndk::ScopedAStatus& ndkStatus) { + if (ndkStatus.isOk()) { + return HalResult::ok(); + } + if (checkUnsupported(ndkStatus)) { + return HalResult::unsupported(); + } + return HalResult::failed(ndkStatus.getDescription()); + } + + template + static HalResult fromReturn(hardware::Return& ret) { + return ret.isOk() ? HalResult::ok() : HalResult::failed(ret.description()); + } + + 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) {} +}; +} // namespace android::power diff --git a/include/powermanager/PowerHalController.h b/include/powermanager/PowerHalController.h index c50bc4a188..7e0bd5bedc 100644 --- a/include/powermanager/PowerHalController.h +++ b/include/powermanager/PowerHalController.h @@ -23,6 +23,7 @@ #include #include #include +#include namespace android { @@ -38,6 +39,7 @@ public: virtual std::unique_ptr connect(); virtual void reset(); + virtual int32_t getAidlVersion(); }; // ------------------------------------------------------------------------------------------------- @@ -59,14 +61,13 @@ public: int32_t durationMs) override; virtual HalResult setMode(aidl::android::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> - createHintSessionWithConfig(int32_t tgid, int32_t uid, const std::vector& threadIds, - int64_t durationNanos, - aidl::android::hardware::power::SessionTag tag, - aidl::android::hardware::power::SessionConfig* config) override; + virtual HalResult> createHintSession( + int32_t tgid, int32_t uid, const std::vector& threadIds, + int64_t durationNanos) override; + virtual HalResult> createHintSessionWithConfig( + int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos, + aidl::android::hardware::power::SessionTag tag, + aidl::android::hardware::power::SessionConfig* config) override; virtual HalResult getHintSessionPreferredRate() override; virtual HalResult getSessionChannel( int tgid, int uid) override; diff --git a/include/powermanager/PowerHalLoader.h b/include/powermanager/PowerHalLoader.h index cbbfa597ba..ab66336738 100644 --- a/include/powermanager/PowerHalLoader.h +++ b/include/powermanager/PowerHalLoader.h @@ -36,6 +36,8 @@ public: static sp loadHidlV1_1(); static sp loadHidlV1_2(); static sp loadHidlV1_3(); + // Returns aidl interface version, or 0 if AIDL is not used + static int32_t getAidlVersion(); private: static std::mutex gHalMutex; @@ -48,6 +50,8 @@ private: static sp loadHidlV1_0Locked() EXCLUSIVE_LOCKS_REQUIRED(gHalMutex); + static int32_t gAidlInterfaceVersion; + PowerHalLoader() = delete; ~PowerHalLoader() = delete; }; diff --git a/include/powermanager/PowerHalWrapper.h b/include/powermanager/PowerHalWrapper.h index e2da014606..6e347a9ce9 100644 --- a/include/powermanager/PowerHalWrapper.h +++ b/include/powermanager/PowerHalWrapper.h @@ -26,6 +26,9 @@ #include #include #include +#include +#include + #include #include @@ -41,134 +44,6 @@ enum class HalSupport { 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(std::forward(value)); } - static HalResult ok(T& value) { return HalResult::ok(T{value}); } - static HalResult failed(std::string msg) { return HalResult(msg, /* unsupported= */ false); } - static HalResult unsupported() { return HalResult("", /* unsupported= */ true); } - - static HalResult fromStatus(const binder::Status& status, T&& data) { - if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { - return HalResult::unsupported(); - } - if (status.isOk()) { - return HalResult::ok(std::forward(data)); - } - return HalResult::failed(std::string(status.toString8().c_str())); - } - - static HalResult fromStatus(const binder::Status& status, T& data) { - return HalResult::fromStatus(status, T{data}); - } - - static HalResult fromStatus(const ndk::ScopedAStatus& status, T&& data) { - if (status.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { - return HalResult::unsupported(); - } - if (status.isOk()) { - return HalResult::ok(std::forward(data)); - } - return HalResult::failed(std::string(status.getDescription())); - } - - static HalResult fromStatus(const ndk::ScopedAStatus& status, T& data) { - return HalResult::fromStatus(status, T{data}); - } - - template - static HalResult fromReturn(hardware::Return& ret, T&& data) { - return ret.isOk() ? HalResult::ok(std::forward(data)) - : HalResult::failed(ret.description()); - } - - template - static HalResult fromReturn(hardware::Return& ret, T& data) { - return HalResult::fromReturn(ret, T{data}); - } - - template - static HalResult fromReturn(hardware::Return& ret, hardware::power::V1_0::Status status, - T&& data) { - return ret.isOk() ? HalResult::fromStatus(status, std::forward(data)) - : HalResult::failed(ret.description()); - } - - template - static HalResult fromReturn(hardware::Return& ret, hardware::power::V1_0::Status status, - T& data) { - return HalResult::fromReturn(ret, 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::move(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(const binder::Status& status) { - if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { - return HalResult::unsupported(); - } - if (status.isOk()) { - return HalResult::ok(); - } - return HalResult::failed(std::string(status.toString8().c_str())); - } - - static HalResult fromStatus(const ndk::ScopedAStatus& status) { - if (status.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { - return HalResult::unsupported(); - } - if (status.isOk()) { - return HalResult::ok(); - } - return HalResult::failed(std::string(status.getDescription())); - } - - template - static HalResult fromReturn(hardware::Return& ret) { - return ret.isOk() ? HalResult::ok() : HalResult::failed(ret.description()); - } - - 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: @@ -177,14 +52,13 @@ public: virtual HalResult setBoost(aidl::android::hardware::power::Boost boost, int32_t durationMs) = 0; virtual HalResult setMode(aidl::android::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> - createHintSessionWithConfig(int32_t tgid, int32_t uid, const std::vector& threadIds, - int64_t durationNanos, - aidl::android::hardware::power::SessionTag tag, - aidl::android::hardware::power::SessionConfig* config) = 0; + virtual HalResult> createHintSession( + int32_t tgid, int32_t uid, const std::vector& threadIds, + int64_t durationNanos) = 0; + virtual HalResult> createHintSessionWithConfig( + int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos, + aidl::android::hardware::power::SessionTag tag, + aidl::android::hardware::power::SessionConfig* config) = 0; virtual HalResult getHintSessionPreferredRate() = 0; virtual HalResult getSessionChannel(int tgid, int uid) = 0; @@ -200,14 +74,13 @@ public: HalResult setBoost(aidl::android::hardware::power::Boost boost, int32_t durationMs) override; HalResult setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; - HalResult> createHintSession( + HalResult> createHintSession( int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos) override; - HalResult> - createHintSessionWithConfig(int32_t tgid, int32_t uid, const std::vector& threadIds, - int64_t durationNanos, - aidl::android::hardware::power::SessionTag tag, - aidl::android::hardware::power::SessionConfig* config) override; + HalResult> createHintSessionWithConfig( + int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos, + aidl::android::hardware::power::SessionTag tag, + aidl::android::hardware::power::SessionConfig* config) override; HalResult getHintSessionPreferredRate() override; HalResult getSessionChannel(int tgid, int uid) override; @@ -285,14 +158,13 @@ public: HalResult setBoost(aidl::android::hardware::power::Boost boost, int32_t durationMs) override; HalResult setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; - HalResult> createHintSession( + HalResult> createHintSession( int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos) override; - HalResult> - createHintSessionWithConfig(int32_t tgid, int32_t uid, const std::vector& threadIds, - int64_t durationNanos, - aidl::android::hardware::power::SessionTag tag, - aidl::android::hardware::power::SessionConfig* config) override; + HalResult> createHintSessionWithConfig( + int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos, + aidl::android::hardware::power::SessionTag tag, + aidl::android::hardware::power::SessionConfig* config) override; HalResult getHintSessionPreferredRate() override; HalResult getSessionChannel(int tgid, @@ -307,14 +179,12 @@ private: std::mutex mBoostMutex; std::mutex mModeMutex; std::shared_ptr mHandle; - // Android framework only sends boost upto DISPLAY_UPDATE_IMMINENT. - // Need to increase the array size if more boost supported. - std::array< - std::atomic, - static_cast(aidl::android::hardware::power::Boost::DISPLAY_UPDATE_IMMINENT) + - 1> + std::array( + *(ndk::enum_range().end() - 1)) + + 1> mBoostSupportedArray GUARDED_BY(mBoostMutex) = {HalSupport::UNKNOWN}; - std::array, + std::array( *(ndk::enum_range().end() - 1)) + 1> diff --git a/include/powermanager/PowerHintSessionWrapper.h b/include/powermanager/PowerHintSessionWrapper.h new file mode 100644 index 0000000000..ba6fe77c80 --- /dev/null +++ b/include/powermanager/PowerHintSessionWrapper.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2024 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. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include "HalResult.h" + +namespace android::power { + +// Wrapper for power hint sessions, which allows for better mocking, +// support checking, and failure handling than using hint sessions directly +class PowerHintSessionWrapper { +public: + virtual ~PowerHintSessionWrapper() = default; + PowerHintSessionWrapper( + std::shared_ptr&& session); + virtual HalResult updateTargetWorkDuration(int64_t in_targetDurationNanos); + virtual HalResult reportActualWorkDuration( + const std::vector<::aidl::android::hardware::power::WorkDuration>& in_durations); + virtual HalResult pause(); + virtual HalResult resume(); + virtual HalResult close(); + virtual HalResult sendHint(::aidl::android::hardware::power::SessionHint in_hint); + virtual HalResult setThreads(const std::vector& in_threadIds); + virtual HalResult setMode(::aidl::android::hardware::power::SessionMode in_type, + bool in_enabled); + virtual HalResult getSessionConfig(); + +private: + std::shared_ptr mSession; + int32_t mInterfaceVersion; +}; + +} // namespace android::power \ No newline at end of file -- cgit v1.2.3-59-g8ed1b