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 --- services/powermanager/PowerHalController.cpp | 57 +++++++++++++++++++++------- 1 file changed, 43 insertions(+), 14 deletions(-) (limited to 'services/powermanager/PowerHalController.cpp') diff --git a/services/powermanager/PowerHalController.cpp b/services/powermanager/PowerHalController.cpp index bc178bce8f..40fd097491 100644 --- a/services/powermanager/PowerHalController.cpp +++ b/services/powermanager/PowerHalController.cpp @@ -57,6 +57,10 @@ void HalConnector::reset() { PowerHalLoader::unloadAll(); } +int32_t HalConnector::getAidlVersion() { + return PowerHalLoader::getAidlVersion(); +} + // ------------------------------------------------------------------------------------------------- void PowerHalController::init() { @@ -77,6 +81,22 @@ std::shared_ptr PowerHalController::initHal() { return mConnectedHal; } +// Using statement expression macro instead of a method lets the static be +// scoped to the outer method while dodging the need for a support lookup table +// This only works for AIDL methods that do not vary supported/unsupported depending +// on their arguments (not setBoost, setMode) which do their own support checks +#define CACHE_SUPPORT(version, method) \ + ({ \ + static bool support = mHalConnector->getAidlVersion() >= version; \ + !support ? decltype(method)::unsupported() : ({ \ + auto result = method; \ + if (result.isUnsupported()) { \ + support = false; \ + } \ + std::move(result); \ + }); \ + }) + // Check if a call to Power HAL function failed; if so, log the failure and // invalidate the current Power HAL handle. template @@ -103,40 +123,49 @@ HalResult PowerHalController::setMode(aidl::android::hardware::power::Mode return processHalResult(handle->setMode(mode, enabled), "setMode"); } -HalResult> -PowerHalController::createHintSession(int32_t tgid, int32_t uid, - const std::vector& threadIds, - int64_t durationNanos) { +// Aidl-only methods + +HalResult> PowerHalController::createHintSession( + int32_t tgid, int32_t uid, const std::vector& threadIds, int64_t durationNanos) { std::shared_ptr handle = initHal(); - return processHalResult(handle->createHintSession(tgid, uid, threadIds, durationNanos), - "createHintSession"); + return CACHE_SUPPORT(2, + processHalResult(handle->createHintSession(tgid, uid, threadIds, + durationNanos), + "createHintSession")); } -HalResult> -PowerHalController::createHintSessionWithConfig( +HalResult> PowerHalController::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) { std::shared_ptr handle = initHal(); - return processHalResult(handle->createHintSessionWithConfig(tgid, uid, threadIds, durationNanos, - tag, config), - "createHintSessionWithConfig"); + return CACHE_SUPPORT(5, + processHalResult(handle->createHintSessionWithConfig(tgid, uid, threadIds, + durationNanos, tag, + config), + "createHintSessionWithConfig")); } HalResult PowerHalController::getHintSessionPreferredRate() { std::shared_ptr handle = initHal(); - return processHalResult(handle->getHintSessionPreferredRate(), "getHintSessionPreferredRate"); + return CACHE_SUPPORT(2, + processHalResult(handle->getHintSessionPreferredRate(), + "getHintSessionPreferredRate")); } HalResult PowerHalController::getSessionChannel( int tgid, int uid) { std::shared_ptr handle = initHal(); - return processHalResult(handle->getSessionChannel(tgid, uid), "getSessionChannel"); + return CACHE_SUPPORT(5, + processHalResult(handle->getSessionChannel(tgid, uid), + "getSessionChannel")); } HalResult PowerHalController::closeSessionChannel(int tgid, int uid) { std::shared_ptr handle = initHal(); - return processHalResult(handle->closeSessionChannel(tgid, uid), "closeSessionChannel"); + return CACHE_SUPPORT(5, + processHalResult(handle->closeSessionChannel(tgid, uid), + "closeSessionChannel")); } } // namespace power -- cgit v1.2.3-59-g8ed1b