Add HWUI session tagging

Adds a hidden method for the creation of special "internal" hint
sessions with extra metadata, and plumbs hwui to use it

Bug: 330553312
Test: atest PerformanceHintNativeTestCases
Test: hwui unit tests
Change-Id: I35e7f81623b8f81a9a12e485f221952a13035b02
diff --git a/native/android/libandroid.map.txt b/native/android/libandroid.map.txt
index 1c203e3..346c87d 100644
--- a/native/android/libandroid.map.txt
+++ b/native/android/libandroid.map.txt
@@ -366,6 +366,7 @@
     APerformanceHint_setIHintManagerForTesting;
     APerformanceHint_sendHint;
     APerformanceHint_getThreadIds;
+    APerformanceHint_createSessionInternal;
     extern "C++" {
         ASurfaceControl_registerSurfaceStatsListener*;
         ASurfaceControl_unregisterSurfaceStatsListener*;
diff --git a/native/android/performance_hint.cpp b/native/android/performance_hint.cpp
index fbb35e2..83056b2 100644
--- a/native/android/performance_hint.cpp
+++ b/native/android/performance_hint.cpp
@@ -463,6 +463,15 @@
     return manager->createSession(threadIds, size, initialTargetWorkDurationNanos);
 }
 
+APerformanceHintSession* APerformanceHint_createSessionInternal(
+        APerformanceHintManager* manager, const int32_t* threadIds, size_t size,
+        int64_t initialTargetWorkDurationNanos, SessionTag tag) {
+    VALIDATE_PTR(manager)
+    VALIDATE_PTR(threadIds)
+    return manager->createSession(threadIds, size, initialTargetWorkDurationNanos,
+                                  static_cast<hal::SessionTag>(tag));
+}
+
 int64_t APerformanceHint_getPreferredUpdateRateNanos(APerformanceHintManager* manager) {
     VALIDATE_PTR(manager)
     return manager->getPreferredRateNanos();
@@ -486,9 +495,9 @@
     delete session;
 }
 
-int APerformanceHint_sendHint(void* session, SessionHint hint) {
+int APerformanceHint_sendHint(APerformanceHintSession* session, SessionHint hint) {
     VALIDATE_PTR(session)
-    return reinterpret_cast<APerformanceHintSession*>(session)->sendHint(hint);
+    return session->sendHint(hint);
 }
 
 int APerformanceHint_setThreads(APerformanceHintSession* session, const pid_t* threadIds,
@@ -498,11 +507,10 @@
     return session->setThreads(threadIds, size);
 }
 
-int APerformanceHint_getThreadIds(void* aPerformanceHintSession, int32_t* const threadIds,
+int APerformanceHint_getThreadIds(APerformanceHintSession* session, int32_t* const threadIds,
                                   size_t* const size) {
-    VALIDATE_PTR(aPerformanceHintSession)
-    return static_cast<APerformanceHintSession*>(aPerformanceHintSession)
-            ->getThreadIds(threadIds, size);
+    VALIDATE_PTR(session)
+    return session->getThreadIds(threadIds, size);
 }
 
 int APerformanceHint_setPreferPowerEfficiency(APerformanceHintSession* session, bool enabled) {
diff --git a/native/android/tests/performance_hint/PerformanceHintNativeTest.cpp b/native/android/tests/performance_hint/PerformanceHintNativeTest.cpp
index 974e6e6..58f56b8 100644
--- a/native/android/tests/performance_hint/PerformanceHintNativeTest.cpp
+++ b/native/android/tests/performance_hint/PerformanceHintNativeTest.cpp
@@ -30,9 +30,7 @@
 #include <memory>
 #include <vector>
 
-using aidl::android::hardware::power::SessionConfig;
-using aidl::android::hardware::power::SessionTag;
-using aidl::android::hardware::power::WorkDuration;
+namespace hal = aidl::android::hardware::power;
 using aidl::android::os::IHintManager;
 using aidl::android::os::IHintSession;
 using ndk::ScopedAStatus;
@@ -45,7 +43,7 @@
 public:
     MOCK_METHOD(ScopedAStatus, createHintSessionWithConfig,
                 (const SpAIBinder& token, const ::std::vector<int32_t>& tids, int64_t durationNanos,
-                 SessionTag tag, std::optional<SessionConfig>* config,
+                 hal::SessionTag tag, std::optional<hal::SessionConfig>* config,
                  std::shared_ptr<IHintSession>* _aidl_return),
                 (override));
     MOCK_METHOD(ScopedAStatus, getHintSessionPreferredRate, (int64_t * _aidl_return), (override));
@@ -71,7 +69,7 @@
     MOCK_METHOD(ScopedAStatus, setMode, (int32_t mode, bool enabled), (override));
     MOCK_METHOD(ScopedAStatus, close, (), (override));
     MOCK_METHOD(ScopedAStatus, reportActualWorkDuration2,
-                (const ::std::vector<WorkDuration>& workDurations), (override));
+                (const ::std::vector<hal::WorkDuration>& workDurations), (override));
     MOCK_METHOD(SpAIBinder, asBinder, (), (override));
     MOCK_METHOD(bool, isRemote, (), (override));
 };
@@ -95,7 +93,7 @@
         return APerformanceHint_getManager();
     }
     APerformanceHintSession* createSession(APerformanceHintManager* manager,
-                                           int64_t targetDuration = 56789L) {
+                                           int64_t targetDuration = 56789L, bool isHwui = false) {
         mMockSession = ndk::SharedRefBase::make<NiceMock<MockIHintSession>>();
         int64_t sessionId = 123;
         std::vector<int32_t> tids;
@@ -104,8 +102,8 @@
 
         ON_CALL(*mMockIHintManager,
                 createHintSessionWithConfig(_, Eq(tids), Eq(targetDuration), _, _, _))
-                .WillByDefault(DoAll(SetArgPointee<4>(
-                                             std::make_optional<SessionConfig>({.id = sessionId})),
+                .WillByDefault(DoAll(SetArgPointee<4>(std::make_optional<hal::SessionConfig>(
+                                             {.id = sessionId})),
                                      SetArgPointee<5>(std::shared_ptr<IHintSession>(mMockSession)),
                                      [] { return ScopedAStatus::ok(); }));
 
@@ -124,6 +122,10 @@
         ON_CALL(*mMockSession, reportActualWorkDuration2(_)).WillByDefault([] {
             return ScopedAStatus::ok();
         });
+        if (isHwui) {
+            return APerformanceHint_createSessionInternal(manager, tids.data(), tids.size(),
+                                                          targetDuration, SessionTag::HWUI);
+        }
         return APerformanceHint_createSession(manager, tids.data(), tids.size(), targetDuration);
     }
 
@@ -131,7 +133,7 @@
     std::shared_ptr<NiceMock<MockIHintSession>> mMockSession = nullptr;
 };
 
-bool equalsWithoutTimestamp(WorkDuration lhs, WorkDuration rhs) {
+bool equalsWithoutTimestamp(hal::WorkDuration lhs, hal::WorkDuration rhs) {
     return lhs.workPeriodStartTimestampNanos == rhs.workPeriodStartTimestampNanos &&
             lhs.cpuDurationNanos == rhs.cpuDurationNanos &&
             lhs.gpuDurationNanos == rhs.gpuDurationNanos && lhs.durationNanos == rhs.durationNanos;
@@ -194,6 +196,16 @@
     APerformanceHint_closeSession(session);
 }
 
+TEST_F(PerformanceHintTest, TestHwuiSessionCreation) {
+    EXPECT_CALL(*mMockIHintManager,
+                createHintSessionWithConfig(_, _, _, hal::SessionTag::HWUI, _, _))
+            .Times(1);
+    APerformanceHintManager* manager = createManager();
+    APerformanceHintSession* session = createSession(manager, 56789L, true);
+    ASSERT_TRUE(session);
+    APerformanceHint_closeSession(session);
+}
+
 TEST_F(PerformanceHintTest, SetThreads) {
     APerformanceHintManager* manager = createManager();
 
@@ -249,8 +261,8 @@
         return false;
     }
     for (int i = 0; i < expected.size(); ++i) {
-        WorkDuration expectedWorkDuration = expected[i];
-        WorkDuration actualWorkDuration = arg[i];
+        hal::WorkDuration expectedWorkDuration = expected[i];
+        hal::WorkDuration actualWorkDuration = arg[i];
         if (!equalsWithoutTimestamp(expectedWorkDuration, actualWorkDuration)) {
             *result_listener << "WorkDuration at [" << i << "] is different: "
                              << "Expected: " << expectedWorkDuration.toString()
@@ -273,7 +285,7 @@
 
     usleep(2); // Sleep for longer than preferredUpdateRateNanos.
     struct TestPair {
-        WorkDuration duration;
+        hal::WorkDuration duration;
         int expectedResult;
     };
     std::vector<TestPair> testPairs{
@@ -282,7 +294,7 @@
             {{1, -20, 1, 13, -8}, EINVAL},
     };
     for (auto&& pair : testPairs) {
-        std::vector<WorkDuration> actualWorkDurations;
+        std::vector<hal::WorkDuration> actualWorkDurations;
         actualWorkDurations.push_back(pair.duration);
 
         EXPECT_CALL(*mMockSession, reportActualWorkDuration2(WorkDurationEq(actualWorkDurations)))