From a8c7e1fc5482d2649c5d79a2e49c481b60584ea8 Mon Sep 17 00:00:00 2001 From: Wei Wang Date: Thu, 18 Jul 2024 12:56:42 +0530 Subject: [PATCH] sm6375-common: power-libperfmgr: add aidl extension server Bug: 151896829 Test: boot flame Change-Id: Ie951008cabe2a5680fbc546a21bdc9a428864ef9 --- power-libperfmgr/Android.bp | 2 + power-libperfmgr/Power.cpp | 86 ++++++++++++----------------- power-libperfmgr/Power.h | 9 ++- power-libperfmgr/PowerExt.cpp | 101 ++++++++++++++++++++++++++++++++++ power-libperfmgr/PowerExt.h | 57 +++++++++++++++++++ power-libperfmgr/service.cpp | 38 ++++++++++++- 6 files changed, 238 insertions(+), 55 deletions(-) create mode 100644 power-libperfmgr/PowerExt.cpp create mode 100644 power-libperfmgr/PowerExt.h diff --git a/power-libperfmgr/Android.bp b/power-libperfmgr/Android.bp index 5a8c89c..0dc2b60 100644 --- a/power-libperfmgr/Android.bp +++ b/power-libperfmgr/Android.bp @@ -28,10 +28,12 @@ cc_binary { "libbinder_ndk", "libdisppower-pixel", "libperfmgr", + "pixel-power-ext-ndk_platform", ], srcs: [ "service.cpp", "Power.cpp", "InteractionHandler.cpp", + "PowerExt.cpp", ], } diff --git a/power-libperfmgr/Power.cpp b/power-libperfmgr/Power.cpp index c8ed22d..a73cd54 100644 --- a/power-libperfmgr/Power.cpp +++ b/power-libperfmgr/Power.cpp @@ -39,63 +39,45 @@ namespace pixel { constexpr char kPowerHalStateProp[] = "vendor.powerhal.state"; constexpr char kPowerHalAudioProp[] = "vendor.powerhal.audio"; -constexpr char kPowerHalInitProp[] = "vendor.powerhal.init"; constexpr char kPowerHalRenderingProp[] = "vendor.powerhal.rendering"; -constexpr char kPowerHalConfigPath[] = "/vendor/etc/powerhint.json"; -Power::Power() - : mHintManager(nullptr), - mInteractionHandler(nullptr), - mVRModeOn(false), - mSustainedPerfModeOn(false), - mReady(false) { - // Parse config but do not start the looper - mHintManager = HintManager::GetFromJSON(kPowerHalConfigPath, false); - if (!mHintManager) { - LOG(FATAL) << "Invalid config: " << kPowerHalConfigPath; +void Power::setReady() { + mInteractionHandler = std::make_unique(mHintManager); + mInteractionHandler->Init(); + + std::string state = ::android::base::GetProperty(kPowerHalStateProp, ""); + if (state == "SUSTAINED_PERFORMANCE") { + ALOGI("Initialize with SUSTAINED_PERFORMANCE on"); + mHintManager->DoHint("SUSTAINED_PERFORMANCE"); + mSustainedPerfModeOn = true; + } else if (state == "VR") { + ALOGI("Initialize with VR on"); + mHintManager->DoHint(state); + mVRModeOn = true; + } else if (state == "VR_SUSTAINED_PERFORMANCE") { + ALOGI("Initialize with SUSTAINED_PERFORMANCE and VR on"); + mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE"); + mSustainedPerfModeOn = true; + mVRModeOn = true; + } else { + ALOGI("Initialize PowerHAL"); } - std::thread initThread([this]() { - ::android::base::WaitForProperty(kPowerHalInitProp, "1"); - mHintManager->Start(); - mInteractionHandler = std::make_unique(mHintManager); - mInteractionHandler->Init(); + state = ::android::base::GetProperty(kPowerHalAudioProp, ""); + if (state == "AUDIO_STREAMING_LOW_LATENCY") { + ALOGI("Initialize with AUDIO_LOW_LATENCY on"); + mHintManager->DoHint(state); + } - std::string state = ::android::base::GetProperty(kPowerHalStateProp, ""); - if (state == "SUSTAINED_PERFORMANCE") { - ALOGI("Initialize with SUSTAINED_PERFORMANCE on"); - mHintManager->DoHint("SUSTAINED_PERFORMANCE"); - mSustainedPerfModeOn = true; - } else if (state == "VR") { - ALOGI("Initialize with VR on"); - mHintManager->DoHint(state); - mVRModeOn = true; - } else if (state == "VR_SUSTAINED_PERFORMANCE") { - ALOGI("Initialize with SUSTAINED_PERFORMANCE and VR on"); - mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE"); - mSustainedPerfModeOn = true; - mVRModeOn = true; - } else { - ALOGI("Initialize PowerHAL"); - } + state = ::android::base::GetProperty(kPowerHalRenderingProp, ""); + if (state == "EXPENSIVE_RENDERING") { + ALOGI("Initialize with EXPENSIVE_RENDERING on"); + mHintManager->DoHint("EXPENSIVE_RENDERING"); + } - state = ::android::base::GetProperty(kPowerHalAudioProp, ""); - if (state == "AUDIO_STREAMING_LOW_LATENCY") { - ALOGI("Initialize with AUDIO_LOW_LATENCY on"); - mHintManager->DoHint(state); - } - - state = ::android::base::GetProperty(kPowerHalRenderingProp, ""); - if (state == "EXPENSIVE_RENDERING") { - ALOGI("Initialize with EXPENSIVE_RENDERING on"); - mHintManager->DoHint("EXPENSIVE_RENDERING"); - } - - // Now start to take powerhint - mReady.store(true); - ALOGI("PowerHAL ready to process hints"); - }); - initThread.detach(); + // Now start to take powerhint + mReady.store(true); + ALOGI("PowerHAL ready to process hints"); } ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) { @@ -229,7 +211,7 @@ ndk::ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) { ndk::ScopedAStatus Power::isBoostSupported(Boost type, bool *_aidl_return) { bool supported = mHintManager->IsHintSupported(toString(type)); - LOG(INFO) << "Power mode " << toString(type) << " isBoostSupported: " << supported; + LOG(INFO) << "Power boost " << toString(type) << " isBoostSupported: " << supported; *_aidl_return = supported; return ndk::ScopedAStatus::ok(); } diff --git a/power-libperfmgr/Power.h b/power-libperfmgr/Power.h index bc69bdc..170f40f 100644 --- a/power-libperfmgr/Power.h +++ b/power-libperfmgr/Power.h @@ -37,12 +37,19 @@ using ::android::perfmgr::HintManager; class Power : public BnPower { public: - Power(); + Power(std::shared_ptr hm) + : mHintManager(hm), + mInteractionHandler(nullptr), + mVRModeOn(false), + mSustainedPerfModeOn(false), + mReady(false) {} + ndk::ScopedAStatus setMode(Mode type, bool enabled) override; ndk::ScopedAStatus isModeSupported(Mode type, bool *_aidl_return) override; ndk::ScopedAStatus setBoost(Boost type, int32_t durationMs) override; ndk::ScopedAStatus isBoostSupported(Boost type, bool *_aidl_return) override; binder_status_t dump(int fd, const char **args, uint32_t numArgs) override; + void setReady(); private: std::shared_ptr mHintManager; diff --git a/power-libperfmgr/PowerExt.cpp b/power-libperfmgr/PowerExt.cpp new file mode 100644 index 0000000..23d4609 --- /dev/null +++ b/power-libperfmgr/PowerExt.cpp @@ -0,0 +1,101 @@ +/* + * 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 ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL) +#define LOG_TAG "android.hardware.power-service.realme_sm6375.ext-libperfmgr" + +#include "PowerExt.h" + +#include + +#include +#include +#include +#include +#include + +#include +#include + +namespace aidl { +namespace android { +namespace hardware { +namespace pixel { +namespace extension { +namespace power { +namespace impl { + +void PowerExt::setReady() { + // Now start to take powerhint + mReady.store(true); + ALOGI("PowerHAL extension ready to process hints"); +} + +ndk::ScopedAStatus PowerExt::setMode(const std::string &mode, bool enabled) { + if (!mReady) { + return ndk::ScopedAStatus::ok(); + } + LOG(DEBUG) << "PowerExt setMode: " << mode << " to: " << enabled; + ATRACE_INT(mode.c_str(), enabled); + + if (enabled) { + mHintManager->DoHint(mode); + } else { + mHintManager->EndHint(mode); + } + + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus PowerExt::isModeSupported(const std::string &mode, bool *_aidl_return) { + bool supported = mHintManager->IsHintSupported(mode); + LOG(INFO) << "PowerExt mode " << mode << " isModeSupported: " << supported; + *_aidl_return = supported; + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus PowerExt::setBoost(const std::string &boost, int32_t durationMs) { + if (!mReady) { + return ndk::ScopedAStatus::ok(); + } + LOG(DEBUG) << "PowerExt setBoost: " << boost << " duration: " << durationMs; + ATRACE_INT(boost.c_str(), durationMs); + + if (durationMs > 0) { + mHintManager->DoHint(boost, std::chrono::milliseconds(durationMs)); + } else if (durationMs == 0) { + mHintManager->DoHint(boost); + } else { + mHintManager->EndHint(boost); + } + + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus PowerExt::isBoostSupported(const std::string &boost, bool *_aidl_return) { + bool supported = mHintManager->IsHintSupported(boost); + LOG(INFO) << "PowerExt boost " << boost << " isBoostSupported: " << supported; + *_aidl_return = supported; + return ndk::ScopedAStatus::ok(); +} + +} // namespace impl +} // namespace power +} // namespace extension +} // namespace pixel +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/power-libperfmgr/PowerExt.h b/power-libperfmgr/PowerExt.h new file mode 100644 index 0000000..b9fc95d --- /dev/null +++ b/power-libperfmgr/PowerExt.h @@ -0,0 +1,57 @@ +/* + * 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. + */ + +#pragma once + +#include +#include +#include + +#include +#include + +namespace aidl { +namespace android { +namespace hardware { +namespace pixel { +namespace extension { +namespace power { +namespace impl { + +using ::android::perfmgr::HintManager; + +class PowerExt : public BnPowerExt { + public: + PowerExt(std::shared_ptr hm) : mHintManager(hm), mReady(false) {} + + ndk::ScopedAStatus setMode(const std::string &mode, bool enabled) override; + ndk::ScopedAStatus isModeSupported(const std::string &mode, bool *_aidl_return) override; + ndk::ScopedAStatus setBoost(const std::string &boost, int32_t durationMs) override; + ndk::ScopedAStatus isBoostSupported(const std::string &boost, bool *_aidl_return) override; + void setReady(); + + private: + std::shared_ptr mHintManager; + std::atomic mReady; +}; + +} // namespace impl +} // namespace power +} // namespace extension +} // namespace pixel +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/power-libperfmgr/service.cpp b/power-libperfmgr/service.cpp index 8c7d496..c0237cd 100644 --- a/power-libperfmgr/service.cpp +++ b/power-libperfmgr/service.cpp @@ -16,24 +16,58 @@ #define LOG_TAG "android.hardware.power-service.moto_sm6375-libperfmgr" -#include "Power.h" +#include #include +#include #include #include +#include "Power.h" +#include "PowerExt.h" + +using aidl::android::hardware::pixel::extension::power::impl::PowerExt; using aidl::android::hardware::power::impl::pixel::Power; +using ::android::perfmgr::HintManager; + +constexpr char kPowerHalConfigPath[] = "/vendor/etc/powerhint.json"; +constexpr char kPowerHalInitProp[] = "vendor.powerhal.init"; int main() { LOG(INFO) << "Power HAL AIDL Service for moto_sm6375 is starting."; + + // Parse config but do not start the looper + std::shared_ptr hm = HintManager::GetFromJSON(kPowerHalConfigPath, false); + if (!hm) { + LOG(FATAL) << "Invalid config: " << kPowerHalConfigPath; + } + + // single thread ABinderProcess_setThreadPoolMaxThreadCount(0); - std::shared_ptr pw = ndk::SharedRefBase::make(); + + // power hal core service + std::shared_ptr pw = ndk::SharedRefBase::make(hm); + ndk::SpAIBinder pwBinder = pw->asBinder(); + + // making the extension service + std::shared_ptr pwExt = ndk::SharedRefBase::make(hm); + + // need to attach the extension to the same binder we will be registering + CHECK(STATUS_OK == AIBinder_setExtension(pwBinder.get(), pwExt->asBinder().get())); const std::string instance = std::string() + Power::descriptor + "/default"; binder_status_t status = AServiceManager_addService(pw->asBinder().get(), instance.c_str()); CHECK(status == STATUS_OK); LOG(INFO) << "Power HAL AIDL Service for moto_sm6375 is started."; + std::thread initThread([&]() { + ::android::base::WaitForProperty(kPowerHalInitProp, "1"); + hm->Start(); + pw->setReady(); + pwExt->setReady(); + }); + initThread.detach(); + ABinderProcess_joinThreadPool(); LOG(ERROR) << "Power HAL AIDL Service for moto_sm6375 died."; return EXIT_FAILURE; // should not reach