power-libperfmgr: allow powerhint calls as soon as service starts

Bug: 147840817
Test: boot
Change-Id: Ic1d6e04109683b999bb99484930e95dc9813fc59
diff --git a/aidl/power-libperfmgr/Power.cpp b/aidl/power-libperfmgr/Power.cpp
index 69d9cd4..90d4189 100644
--- a/aidl/power-libperfmgr/Power.cpp
+++ b/aidl/power-libperfmgr/Power.cpp
@@ -41,7 +41,11 @@
 constexpr char kPowerHalAudioProp[] = "vendor.powerhal.audio";
 constexpr char kPowerHalRenderingProp[] = "vendor.powerhal.rendering";
 
-void Power::setReady() {
+Power::Power(std::shared_ptr<HintManager> hm)
+    : mHintManager(hm),
+      mInteractionHandler(nullptr),
+      mVRModeOn(false),
+      mSustainedPerfModeOn(false) {
     mInteractionHandler = std::make_unique<InteractionHandler>(mHintManager);
     mInteractionHandler->Init();
 
@@ -76,14 +80,10 @@
     }
 
     // Now start to take powerhint
-    mReady.store(true);
     ALOGI("PowerHAL ready to process hints");
 }
 
 ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) {
-    if (!mReady) {
-        return ndk::ScopedAStatus::ok();
-    }
     LOG(DEBUG) << "Power setMode: " << toString(type) << " to: " << enabled;
     ATRACE_INT(toString(type).c_str(), enabled);
     switch (type) {
@@ -170,9 +170,6 @@
 }
 
 ndk::ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) {
-    if (!mReady) {
-        return ndk::ScopedAStatus::ok();
-    }
     LOG(DEBUG) << "Power setBoost: " << toString(type) << " duration: " << durationMs;
     ATRACE_INT(toString(type).c_str(), durationMs);
     switch (type) {
@@ -221,20 +218,18 @@
 }
 
 binder_status_t Power::dump(int fd, const char **, uint32_t) {
-    if (mReady) {
-        std::string buf(::android::base::StringPrintf(
-                "HintManager Running: %s\n"
-                "VRMode: %s\n"
-                "SustainedPerformanceMode: %s\n",
-                boolToString(mHintManager->IsRunning()), boolToString(mVRModeOn),
-                boolToString(mSustainedPerfModeOn)));
-        // Dump nodes through libperfmgr
-        mHintManager->DumpToFd(fd);
-        if (!::android::base::WriteStringToFd(buf, fd)) {
-            PLOG(ERROR) << "Failed to dump state to fd";
-        }
-        fsync(fd);
+    std::string buf(::android::base::StringPrintf(
+            "HintManager Running: %s\n"
+            "VRMode: %s\n"
+            "SustainedPerformanceMode: %s\n",
+            boolToString(mHintManager->IsRunning()), boolToString(mVRModeOn),
+            boolToString(mSustainedPerfModeOn)));
+    // Dump nodes through libperfmgr
+    mHintManager->DumpToFd(fd);
+    if (!::android::base::WriteStringToFd(buf, fd)) {
+        PLOG(ERROR) << "Failed to dump state to fd";
     }
+    fsync(fd);
     return STATUS_OK;
 }
 
diff --git a/aidl/power-libperfmgr/Power.h b/aidl/power-libperfmgr/Power.h
index 170f40f..d9ef553 100644
--- a/aidl/power-libperfmgr/Power.h
+++ b/aidl/power-libperfmgr/Power.h
@@ -37,26 +37,18 @@
 
 class Power : public BnPower {
   public:
-    Power(std::shared_ptr<HintManager> hm)
-        : mHintManager(hm),
-          mInteractionHandler(nullptr),
-          mVRModeOn(false),
-          mSustainedPerfModeOn(false),
-          mReady(false) {}
-
+    Power(std::shared_ptr<HintManager> hm);
     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<HintManager> mHintManager;
     std::unique_ptr<InteractionHandler> mInteractionHandler;
     std::atomic<bool> mVRModeOn;
     std::atomic<bool> mSustainedPerfModeOn;
-    std::atomic<bool> mReady;
 };
 
 }  // namespace pixel
diff --git a/aidl/power-libperfmgr/PowerExt.cpp b/aidl/power-libperfmgr/PowerExt.cpp
index 06232fd..2618ef2 100644
--- a/aidl/power-libperfmgr/PowerExt.cpp
+++ b/aidl/power-libperfmgr/PowerExt.cpp
@@ -38,16 +38,7 @@
 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);
 
@@ -68,9 +59,6 @@
 }
 
 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);
 
diff --git a/aidl/power-libperfmgr/PowerExt.h b/aidl/power-libperfmgr/PowerExt.h
index b9fc95d..9983dc6 100644
--- a/aidl/power-libperfmgr/PowerExt.h
+++ b/aidl/power-libperfmgr/PowerExt.h
@@ -35,17 +35,14 @@
 
 class PowerExt : public BnPowerExt {
   public:
-    PowerExt(std::shared_ptr<HintManager> hm) : mHintManager(hm), mReady(false) {}
-
+    PowerExt(std::shared_ptr<HintManager> hm) : mHintManager(hm) {}
     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<HintManager> mHintManager;
-    std::atomic<bool> mReady;
 };
 
 }  // namespace impl
diff --git a/aidl/power-libperfmgr/service.cpp b/aidl/power-libperfmgr/service.cpp
index fc509d7..40e7add 100644
--- a/aidl/power-libperfmgr/service.cpp
+++ b/aidl/power-libperfmgr/service.cpp
@@ -34,7 +34,7 @@
 constexpr char kPowerHalInitProp[] = "vendor.powerhal.init";
 
 int main() {
-    LOG(INFO) << "Xiaomi Power HAL AIDL Service is starting.";
+    LOG(INFO) << "Xiaomi Power HAL AIDL Service with Extension is starting.";
 
     // Parse config but do not start the looper
     std::shared_ptr<HintManager> hm = HintManager::GetFromJSON(kPowerHalConfigPath, false);
@@ -45,14 +45,14 @@
     // single thread
     ABinderProcess_setThreadPoolMaxThreadCount(0);
 
-    // power hal core service
+    // core service
     std::shared_ptr<Power> pw = ndk::SharedRefBase::make<Power>(hm);
     ndk::SpAIBinder pwBinder = pw->asBinder();
 
-    // making the extension service
+    // extension service
     std::shared_ptr<PowerExt> pwExt = ndk::SharedRefBase::make<PowerExt>(hm);
 
-    // need to attach the extension to the same binder we will be registering
+    // 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";
@@ -63,12 +63,12 @@
     std::thread initThread([&]() {
         ::android::base::WaitForProperty(kPowerHalInitProp, "1");
         hm->Start();
-        pw->setReady();
-        pwExt->setReady();
     });
     initThread.detach();
 
     ABinderProcess_joinThreadPool();
-    LOG(ERROR) << "Xiaomi Power HAL AIDL Service died.";
-    return EXIT_FAILURE;  // should not reach
+
+    // should not reach
+    LOG(ERROR) << "Xiaomi Power HAL AIDL Service with Extension just died.";
+    return EXIT_FAILURE;
 }