Update CPU GPU headroom HAL API

* change pid to tids list
* add configurable interval value
* change float (array) return type to union type
* add default values for the params

Bug: 346604998
Flag: EXEMPT HAL changes
Test: atest VtsHalPowerTargetTest
Change-Id: I4422b8489da5bf072ad821d04f91fcfabccf9d98
diff --git a/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/CpuHeadroomParams.aidl b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/CpuHeadroomParams.aidl
index 09a2ace..de92105 100644
--- a/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/CpuHeadroomParams.aidl
+++ b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/CpuHeadroomParams.aidl
@@ -34,9 +34,10 @@
 package android.hardware.power;
 @JavaDerive(equals=true, toString=true) @VintfStability
 parcelable CpuHeadroomParams {
-  android.hardware.power.CpuHeadroomParams.CalculationType calculationType;
-  android.hardware.power.CpuHeadroomParams.SelectionType selectionType;
-  int pid;
+  android.hardware.power.CpuHeadroomParams.CalculationType calculationType = android.hardware.power.CpuHeadroomParams.CalculationType.MIN;
+  int calculationWindowMillis = 1000;
+  android.hardware.power.CpuHeadroomParams.SelectionType selectionType = android.hardware.power.CpuHeadroomParams.SelectionType.ALL;
+  int[] tids;
   enum CalculationType {
     MIN,
     AVERAGE,
diff --git a/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/CpuHeadroomResult.aidl b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/CpuHeadroomResult.aidl
new file mode 100644
index 0000000..9303906
--- /dev/null
+++ b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/CpuHeadroomResult.aidl
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.hardware.power;
+@JavaDerive(equals=true, toString=true) @VintfStability
+union CpuHeadroomResult {
+  float globalHeadroom;
+  float[] perCoreHeadroom;
+}
diff --git a/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/GpuHeadroomParams.aidl b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/GpuHeadroomParams.aidl
index 64bb4a4..6faa938 100644
--- a/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/GpuHeadroomParams.aidl
+++ b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/GpuHeadroomParams.aidl
@@ -34,7 +34,8 @@
 package android.hardware.power;
 @JavaDerive(equals=true, toString=true) @VintfStability
 parcelable GpuHeadroomParams {
-  android.hardware.power.GpuHeadroomParams.CalculationType calculationType;
+  android.hardware.power.GpuHeadroomParams.CalculationType calculationType = android.hardware.power.GpuHeadroomParams.CalculationType.MIN;
+  int calculationWindowMillis = 1000;
   enum CalculationType {
     MIN,
     AVERAGE,
diff --git a/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/GpuHeadroomResult.aidl b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/GpuHeadroomResult.aidl
new file mode 100644
index 0000000..bbd3d8b
--- /dev/null
+++ b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/GpuHeadroomResult.aidl
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.hardware.power;
+@JavaDerive(equals=true, toString=true) @VintfStability
+union GpuHeadroomResult {
+  float globalHeadroom;
+}
diff --git a/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/IPower.aidl b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/IPower.aidl
index a855ddd..9d20ca1 100644
--- a/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/IPower.aidl
+++ b/power/aidl/aidl_api/android.hardware.power/current/android/hardware/power/IPower.aidl
@@ -44,8 +44,8 @@
   android.hardware.power.ChannelConfig getSessionChannel(in int tgid, in int uid);
   oneway void closeSessionChannel(in int tgid, in int uid);
   android.hardware.power.SupportInfo getSupportInfo();
-  float[] getCpuHeadroom(in android.hardware.power.CpuHeadroomParams params);
-  float getGpuHeadroom(in android.hardware.power.GpuHeadroomParams params);
+  android.hardware.power.CpuHeadroomResult getCpuHeadroom(in android.hardware.power.CpuHeadroomParams params);
+  android.hardware.power.GpuHeadroomResult getGpuHeadroom(in android.hardware.power.GpuHeadroomParams params);
   long getCpuHeadroomMinIntervalMillis();
   long getGpuHeadroomMinIntervalMillis();
   oneway void sendCompositionData(in android.hardware.power.CompositionData[] data);
diff --git a/power/aidl/android/hardware/power/CpuHeadroomParams.aidl b/power/aidl/android/hardware/power/CpuHeadroomParams.aidl
index cf71b67..64c7b44 100644
--- a/power/aidl/android/hardware/power/CpuHeadroomParams.aidl
+++ b/power/aidl/android/hardware/power/CpuHeadroomParams.aidl
@@ -32,7 +32,14 @@
     /**
      * The calculation type.
      */
-    CalculationType calculationType;
+    CalculationType calculationType = CalculationType.MIN;
+
+    /**
+     * The calculation rolling window size in milliseconds.
+     * The device should support a superset of [50, 10000] and try to use the closest feasible
+     * window size to the provided value param.
+     */
+    int calculationWindowMillis = 1000;
 
     /**
      * Defines how to select the CPU.
@@ -47,16 +54,17 @@
     /**
      * The CPU selection type.
      */
-    SelectionType selectionType;
+    SelectionType selectionType = SelectionType.ALL;
 
     /**
-     * The caller thread's PID.
+     * The thread TIDs to track.
      *
-     * If pid is positive, return the headroom only for cores that are available
-     * to the given pid, otherwise return the headroom(s) for all cores.
+     * If tids are not-empty, return the headrooms only for cores that are available
+     * to the given tids, otherwise return the headroom(s) for all cores.
      *
-     * This should handle all the cases including but not limited to thread core
-     * affinity and app cpuset that change the available CPU cores for the caller.
+     * This should handle all the cases including but not limited to core affinity and app cpuset
+     * that change the available CPU cores for the caller. And the HAL should check that the TIDs
+     * have the same core affinity.
      */
-    int pid;
+    int[] tids;
 }
diff --git a/power/aidl/android/hardware/power/CpuHeadroomResult.aidl b/power/aidl/android/hardware/power/CpuHeadroomResult.aidl
new file mode 100644
index 0000000..316d5f6
--- /dev/null
+++ b/power/aidl/android/hardware/power/CpuHeadroomResult.aidl
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ */
+
+package android.hardware.power;
+
+/**
+ * Headroom value result depending on the request params.
+ *
+ * Each value is ranged from [0, 100], where 0 indicates no CPU resources were left
+ * during the calculation interval and the app may expect low resources to be granted.
+ */
+@VintfStability
+@JavaDerive(equals=true, toString=true)
+union CpuHeadroomResult {
+    /**
+     * If ALL selection type is requested.
+     */
+    float globalHeadroom;
+    /**
+     * If PER_CORE selection type is requested.
+     */
+    float[] perCoreHeadroom;
+}
diff --git a/power/aidl/android/hardware/power/GpuHeadroomParams.aidl b/power/aidl/android/hardware/power/GpuHeadroomParams.aidl
index 972adbc..68848d8 100644
--- a/power/aidl/android/hardware/power/GpuHeadroomParams.aidl
+++ b/power/aidl/android/hardware/power/GpuHeadroomParams.aidl
@@ -32,5 +32,11 @@
     /**
      * The calculation type.
      */
-    CalculationType calculationType;
+    CalculationType calculationType = CalculationType.MIN;
+
+    /**
+     * The device should support a superset of [50, 10000] and try to use the closest feasible
+     * window size to the provided value param.
+     */
+    int calculationWindowMillis = 1000;
 }
diff --git a/power/aidl/android/hardware/power/GpuHeadroomResult.aidl b/power/aidl/android/hardware/power/GpuHeadroomResult.aidl
new file mode 100644
index 0000000..ef3257d
--- /dev/null
+++ b/power/aidl/android/hardware/power/GpuHeadroomResult.aidl
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+
+package android.hardware.power;
+
+/**
+ * Headroom value result depending on the request params.
+ *
+ * Each value is ranged from [0, 100], where 0 indicates no GPU resources were left
+ * during the calculation interval and the app may expect low resources to be granted.
+ */
+@VintfStability
+@JavaDerive(equals=true, toString=true)
+union GpuHeadroomResult {
+    float globalHeadroom;
+}
diff --git a/power/aidl/android/hardware/power/IPower.aidl b/power/aidl/android/hardware/power/IPower.aidl
index db1d93f..41a9379 100644
--- a/power/aidl/android/hardware/power/IPower.aidl
+++ b/power/aidl/android/hardware/power/IPower.aidl
@@ -21,7 +21,9 @@
 import android.hardware.power.CompositionData;
 import android.hardware.power.CompositionUpdate;
 import android.hardware.power.CpuHeadroomParams;
+import android.hardware.power.CpuHeadroomResult;
 import android.hardware.power.GpuHeadroomParams;
+import android.hardware.power.GpuHeadroomResult;
 import android.hardware.power.IPowerHintSession;
 import android.hardware.power.Mode;
 import android.hardware.power.SessionConfig;
@@ -162,24 +164,21 @@
      * Provides an estimate of available CPU headroom the device based on past history.
      * <p>
      * @param params params to customize the CPU headroom calculation
-     * @return a single value or an array of values depending on selection type of params.
-     *         Each value is ranged from [0, 100], and 0 indicates no CPU resources were left
-     *         during the calculation interval and the app may expect low resources to be granted.
      * @throws EX_UNSUPPORTED_OPERATION if the API is unsupported or the request params can't be
      *         served.
+     * @throws EX_SECURITY if the TIDs passed in do not belong to the same process.
+     * @throws EX_ILLEGAL_STATE if the TIDs passed in do not have the same core affinity setting.
      */
-    float[] getCpuHeadroom(in CpuHeadroomParams params);
+    CpuHeadroomResult getCpuHeadroom(in CpuHeadroomParams params);
 
     /**
      * Provides an estimate of available GPU headroom the device based on past history.
      * <p>
      * @param params params to customize the GPU headroom calculation
-     * @return Value is ranged from [0, 100], and 0 indicates no GPU resources were left
-     *         during the calculation interval and the app may expect low resources to be granted.
      * @throws EX_UNSUPPORTED_OPERATION if the API is unsupported or the request params can't be
      *         served.
      */
-    float getGpuHeadroom(in GpuHeadroomParams params);
+    GpuHeadroomResult getGpuHeadroom(in GpuHeadroomParams params);
 
     /**
      * Minimum polling interval for calling getCpuHeadroom in milliseconds.
diff --git a/power/aidl/default/Power.cpp b/power/aidl/default/Power.cpp
index d9353fe..8490b62 100644
--- a/power/aidl/default/Power.cpp
+++ b/power/aidl/default/Power.cpp
@@ -71,14 +71,22 @@
     return ScopedAStatus::ok();
 }
 
-ndk::ScopedAStatus Power::getCpuHeadroom(const CpuHeadroomParams& _,
-                                         std::vector<float>* _aidl_return) {
-    *_aidl_return = {0.5f};
-    return ndk::ScopedAStatus::ok();
+ndk::ScopedAStatus Power::getCpuHeadroom(const CpuHeadroomParams& params,
+                                         CpuHeadroomResult* _aidl_return) {
+    if (params.selectionType == CpuHeadroomParams::SelectionType::ALL) {
+        _aidl_return->set<CpuHeadroomResult::globalHeadroom>(100.0f);
+        return ndk::ScopedAStatus::ok();
+    } else if (params.selectionType == CpuHeadroomParams::SelectionType::PER_CORE) {
+        std::vector<float> headroom = {50.0f, 100.0f};
+        _aidl_return->set<CpuHeadroomResult::perCoreHeadroom>(headroom);
+        return ndk::ScopedAStatus::ok();
+    }
+    return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
 }
 
-ndk::ScopedAStatus Power::getGpuHeadroom(const GpuHeadroomParams& _, float* _aidl_return) {
-    *_aidl_return = 0.5f;
+ndk::ScopedAStatus Power::getGpuHeadroom(const GpuHeadroomParams& _,
+                                         GpuHeadroomResult* _aidl_return) {
+    _aidl_return->set<GpuHeadroomResult::globalHeadroom>(100.0f);
     return ndk::ScopedAStatus::ok();
 }
 
diff --git a/power/aidl/default/Power.h b/power/aidl/default/Power.h
index 51528c6..c4aa7ab 100644
--- a/power/aidl/default/Power.h
+++ b/power/aidl/default/Power.h
@@ -46,9 +46,10 @@
     ndk::ScopedAStatus closeSessionChannel(int32_t tgid, int32_t uid) override;
     ndk::ScopedAStatus getSupportInfo(SupportInfo* _aidl_return) override;
     ndk::ScopedAStatus getCpuHeadroom(const CpuHeadroomParams& params,
-                                      std::vector<float>* _aidl_return) override;
+                                      CpuHeadroomResult* _aidl_return) override;
+
     ndk::ScopedAStatus getGpuHeadroom(const GpuHeadroomParams& params,
-                                      float* _aidl_return) override;
+                                      GpuHeadroomResult* _aidl_return) override;
     ndk::ScopedAStatus getCpuHeadroomMinIntervalMillis(int64_t* _aidl_return) override;
     ndk::ScopedAStatus getGpuHeadroomMinIntervalMillis(int64_t* _aidl_return) override;
     ndk::ScopedAStatus sendCompositionData(const std::vector<CompositionData>& in_data) override;
diff --git a/power/aidl/vts/VtsHalPowerTargetTest.cpp b/power/aidl/vts/VtsHalPowerTargetTest.cpp
index 0df360a..ffe20c6 100644
--- a/power/aidl/vts/VtsHalPowerTargetTest.cpp
+++ b/power/aidl/vts/VtsHalPowerTargetTest.cpp
@@ -45,7 +45,9 @@
 using android::hardware::power::ChannelConfig;
 using android::hardware::power::ChannelMessage;
 using android::hardware::power::CpuHeadroomParams;
+using android::hardware::power::CpuHeadroomResult;
 using android::hardware::power::GpuHeadroomParams;
+using android::hardware::power::GpuHeadroomResult;
 using android::hardware::power::IPower;
 using android::hardware::power::IPowerHintSession;
 using android::hardware::power::Mode;
@@ -307,7 +309,7 @@
         GTEST_SKIP() << "DEVICE not launching with Power V6 and beyond.";
     }
     CpuHeadroomParams params;
-    std::vector<float> headroom;
+    CpuHeadroomResult headroom;
     auto ret = power->getCpuHeadroom(params, &headroom);
     if (ret.getExceptionCode() == EX_UNSUPPORTED_OPERATION) {
         GTEST_SKIP() << "power->getCpuHeadroom is not supported";
@@ -316,9 +318,9 @@
     int64_t minIntervalMillis;
     ASSERT_TRUE(power->getCpuHeadroomMinIntervalMillis(&minIntervalMillis).isOk());
     ASSERT_GE(minIntervalMillis, 0);
-    ASSERT_GE(headroom.size(), 1);
-    ASSERT_GE(headroom[0], 0.0f);
-    ASSERT_LE(headroom[0], 100.00f);
+    ASSERT_EQ(headroom.getTag(), CpuHeadroomResult::globalHeadroom);
+    ASSERT_GE(headroom.get<CpuHeadroomResult::globalHeadroom>(), 0.0f);
+    ASSERT_LE(headroom.get<CpuHeadroomResult::globalHeadroom>(), 100.00f);
 }
 
 TEST_P(PowerAidl, getGpuHeadroom) {
@@ -326,7 +328,7 @@
         GTEST_SKIP() << "DEVICE not launching with Power V6 and beyond.";
     }
     GpuHeadroomParams params;
-    float headroom;
+    GpuHeadroomResult headroom;
     auto ret = power->getGpuHeadroom(params, &headroom);
     if (ret.getExceptionCode() == EX_UNSUPPORTED_OPERATION) {
         GTEST_SKIP() << "power->getGpuHeadroom is not supported";
@@ -335,8 +337,9 @@
     int64_t minIntervalMillis;
     ASSERT_TRUE(power->getGpuHeadroomMinIntervalMillis(&minIntervalMillis).isOk());
     ASSERT_GE(minIntervalMillis, 0);
-    ASSERT_GE(headroom, 0.0f);
-    ASSERT_LE(headroom, 100.00f);
+    ASSERT_EQ(headroom.getTag(), GpuHeadroomResult::globalHeadroom);
+    ASSERT_GE(headroom.get<GpuHeadroomResult::globalHeadroom>(), 0.0f);
+    ASSERT_LE(headroom.get<GpuHeadroomResult::globalHeadroom>(), 100.00f);
 }
 
 // FIXED_PERFORMANCE mode is required for all devices which ship on Android 11