Power: make powerhal starts handling powerhint after boot_complete

some sysfs node may not be ready before boot_complete:
e.g. we are delaying bw_hwmon in favor of boot time

Bug: 80321544
Test: Boot and check powerhint handling
Change-Id: I8d2cfa332d9cf9fbafdfa3f48ffff4105c6bcaae
diff --git a/init.hardware.rc b/init.hardware.rc
index 5fcd03a..4a9ea53 100644
--- a/init.hardware.rc
+++ b/init.hardware.rc
@@ -569,6 +569,8 @@
     chown system system /sys/devices/soc/soc:qcom,cpubw/devfreq/soc:qcom,cpubw/bw_hwmon/hyst_trigger_count
     chown system system /sys/devices/soc/soc:qcom,cpubw/devfreq/soc:qcom,cpubw/bw_hwmon/hist_memory
     chown system system /sys/devices/soc/soc:qcom,cpubw/devfreq/soc:qcom,cpubw/bw_hwmon/hyst_length
+    # Enable PowerHAL hint processing
+    setprop vendor.powerhal.init 1
 
 on property:sys.boot_completed=1
     # Enable power setting and set sys.post_boot.parsed to 1
diff --git a/power-libperfmgr/Power.cpp b/power-libperfmgr/Power.cpp
index c0fadac..3dca906 100644
--- a/power-libperfmgr/Power.cpp
+++ b/power-libperfmgr/Power.cpp
@@ -52,40 +52,51 @@
 using ::android::hardware::Void;
 
 Power::Power() :
-        mHintManager(HintManager::GetFromJSON("/vendor/etc/powerhint.json")),
-        mInteractionHandler(mHintManager),
+        mHintManager(nullptr),
+        mInteractionHandler(nullptr),
         mVRModeOn(false),
         mSustainedPerfModeOn(false),
-        mEncoderModeOn(false) {
-    mInteractionHandler.Init();
+        mEncoderModeOn(false),
+        mReady(false) {
+            mInitThread =
+            std::thread([this](){
+                            android::base::WaitForProperty(kPowerHalInitProp, "1");
+                            mHintManager = HintManager::GetFromJSON("/vendor/etc/powerhint.json");
+                            mInteractionHandler = std::make_unique<InteractionHandler>(mHintManager);
+                            mInteractionHandler->Init();
 
-    std::string state = android::base::GetProperty(kPowerHalStateProp, "");
-    if (state == "VIDEO_ENCODE") {
-        ALOGI("Initialize with VIDEO_ENCODE on");
-        mHintManager->DoHint("VIDEO_ENCODE");
-        mEncoderModeOn = true;
-    } else if (state ==  "SUSTAINED_PERFORMANCE") {
-        ALOGI("Initialize with SUSTAINED_PERFORMANCE on");
-        mHintManager->DoHint("SUSTAINED_PERFORMANCE");
-        mSustainedPerfModeOn = true;
-    } else if (state == "VR_MODE") {
-        ALOGI("Initialize with VR_MODE on");
-        mHintManager->DoHint("VR_MODE");
-        mVRModeOn = true;
-    } else if (state == "VR_SUSTAINED_PERFORMANCE") {
-        ALOGI("Initialize with SUSTAINED_PERFORMANCE and VR_MODE on");
-        mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE");
-        mSustainedPerfModeOn = true;
-        mVRModeOn = true;
-    } else {
-        ALOGI("Initialize PowerHAL");
-    }
+                            std::string state = android::base::GetProperty(kPowerHalStateProp, "");
+                            if (state == "VIDEO_ENCODE") {
+                                ALOGI("Initialize with VIDEO_ENCODE on");
+                                mHintManager->DoHint("VIDEO_ENCODE");
+                                mEncoderModeOn = true;
+                            } else if (state ==  "SUSTAINED_PERFORMANCE") {
+                                ALOGI("Initialize with SUSTAINED_PERFORMANCE on");
+                                mHintManager->DoHint("SUSTAINED_PERFORMANCE");
+                                mSustainedPerfModeOn = true;
+                            } else if (state == "VR_MODE") {
+                                ALOGI("Initialize with VR_MODE on");
+                                mHintManager->DoHint("VR_MODE");
+                                mVRModeOn = true;
+                            } else if (state == "VR_SUSTAINED_PERFORMANCE") {
+                                ALOGI("Initialize with SUSTAINED_PERFORMANCE and VR_MODE on");
+                                mHintManager->DoHint("VR_SUSTAINED_PERFORMANCE");
+                                mSustainedPerfModeOn = true;
+                                mVRModeOn = true;
+                            } else {
+                                ALOGI("Initialize PowerHAL");
+                            }
 
-    state = android::base::GetProperty(kPowerHalAudioProp, "");
-    if (state == "LOW_LATENCY") {
-        ALOGI("Initialize with AUDIO_LOW_LATENCY on");
-        mHintManager->DoHint("AUDIO_LOW_LATENCY");
-    }
+                            state = android::base::GetProperty(kPowerHalAudioProp, "");
+                            if (state == "LOW_LATENCY") {
+                                ALOGI("Initialize with AUDIO_LOW_LATENCY on");
+                                mHintManager->DoHint("AUDIO_LOW_LATENCY");
+                            }
+
+                            // Now start to take powerhint
+                            mReady.store(true);
+                        });
+            mInitThread.detach();
 }
 
 // Methods from ::android::hardware::power::V1_0::IPower follow.
@@ -94,7 +105,7 @@
 }
 
 Return<void> Power::powerHint(PowerHint_1_0 hint, int32_t data) {
-    if (!isSupportedGovernor()) {
+    if (!isSupportedGovernor() || !mReady) {
         return Void();
     }
 
@@ -103,7 +114,7 @@
             if (mVRModeOn || mSustainedPerfModeOn) {
                 ALOGV("%s: ignoring due to other active perf hints", __func__);
             } else {
-                mInteractionHandler.Acquire(data);
+                mInteractionHandler->Acquire(data);
             }
             break;
         case PowerHint_1_0::VIDEO_ENCODE:
@@ -417,7 +428,7 @@
 
 // Methods from ::android::hardware::power::V1_2::IPower follow.
 Return<void> Power::powerHintAsync_1_2(PowerHint_1_2 hint, int32_t data) {
-    if (!isSupportedGovernor()) {
+    if (!isSupportedGovernor() || !mReady) {
         return Void();
     }
 
@@ -514,7 +525,7 @@
 }
 
 Return<void> Power::debug(const hidl_handle& handle, const hidl_vec<hidl_string>&) {
-    if (handle != nullptr && handle->numFds >= 1) {
+    if (handle != nullptr && handle->numFds >= 1 && mReady) {
         int fd = handle->data[0];
 
         std::string buf(android::base::StringPrintf("HintManager Running: %s\n"
diff --git a/power-libperfmgr/Power.h b/power-libperfmgr/Power.h
index a39323f..61529b2 100644
--- a/power-libperfmgr/Power.h
+++ b/power-libperfmgr/Power.h
@@ -18,6 +18,7 @@
 #define ANDROID_HARDWARE_POWER_V1_2_POWER_H
 
 #include <atomic>
+#include <thread>
 
 #include <android/hardware/power/1.2/IPower.h>
 #include <hidl/MQDescriptor.h>
@@ -43,6 +44,7 @@
 
 constexpr char kPowerHalStateProp[] = "vendor.powerhal.state";
 constexpr char kPowerHalAudioProp[] = "vendor.powerhal.audio";
+constexpr char kPowerHalInitProp[] = "vendor.powerhal.init";
 
 struct Power : public IPower {
     // Methods from ::android::hardware::power::V1_0::IPower follow.
@@ -68,10 +70,12 @@
     static bool isSupportedGovernor();
 
     std::shared_ptr<HintManager> mHintManager;
-    InteractionHandler mInteractionHandler;
+    std::unique_ptr<InteractionHandler> mInteractionHandler;
     std::atomic<bool> mVRModeOn;
     std::atomic<bool> mSustainedPerfModeOn;
     std::atomic<bool> mEncoderModeOn;
+    std::atomic<bool> mReady;
+    std::thread mInitThread;
 };
 
 }  // namespace implementation
diff --git a/sepolicy/vendor/property_contexts b/sepolicy/vendor/property_contexts
index 80239a4..0caae4e 100644
--- a/sepolicy/vendor/property_contexts
+++ b/sepolicy/vendor/property_contexts
@@ -25,6 +25,7 @@
 sys.logger.bluetooth       u:object_r:bluetooth_log_prop:s0
 vendor.powerhal.state      u:object_r:power_prop:s0
 vendor.powerhal.audio      u:object_r:power_prop:s0
+vendor.powerhal.init       u:object_r:power_prop:s0
 sys.wlan.driver.version    u:object_r:vendor_wifi_version:s0
 sys.wlan.firmware.version  u:object_r:vendor_wifi_version:s0
 persist.vendor.usb.config  u:object_r:vendor_usb_config_prop:s0