GpuStats: send GpuStats at activity launch hint

USAP preloads the driver in usap pool, and those processes in the pool
are not bound with any applications. Previously we send GpuStats based
on the completion of driver loading. So with usap enabled, we won't be
able to receive stats for system built-in driver.

If we send the stats when all the existing stats fields are filled,
there will be tons of selinux violations, because those non app
processes are trying to send the stats as well.

So we end up sending the stats based on the hint of activity launch.

Bug: 131866357
Test: dumpsys gpu with enable/disable usap and check the selinux
Change-Id: Ib1291f3bb82d4cd1c1a893d8ea1db3090fe587bd
diff --git a/libs/graphicsenv/GraphicsEnv.cpp b/libs/graphicsenv/GraphicsEnv.cpp
index 2bfd908..2f84f86 100644
--- a/libs/graphicsenv/GraphicsEnv.cpp
+++ b/libs/graphicsenv/GraphicsEnv.cpp
@@ -160,6 +160,22 @@
     mSphalLibraries = sphalLibraries;
 }
 
+void GraphicsEnv::hintActivityLaunch() {
+    ATRACE_CALL();
+
+    // If there's already graphics driver preloaded in the process, just send
+    // the stats info to GpuStats directly through async binder.
+    std::lock_guard<std::mutex> lock(mStatsLock);
+    if (mGpuStats.glDriverToSend) {
+        mGpuStats.glDriverToSend = false;
+        sendGpuStatsLocked(GraphicsEnv::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
+    }
+    if (mGpuStats.vkDriverToSend) {
+        mGpuStats.vkDriverToSend = false;
+        sendGpuStatsLocked(GraphicsEnv::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
+    }
+}
+
 void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
                               const std::string& driverVersionName, uint64_t driverVersionCode,
                               int64_t driverBuildTime, const std::string& appPackageName,
@@ -220,23 +236,21 @@
     }
 }
 
-void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isLoaded, int64_t driverLoadingTime) {
+void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isDriverLoaded,
+                                  int64_t driverLoadingTime) {
     ATRACE_CALL();
 
     std::lock_guard<std::mutex> lock(mStatsLock);
-    GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
-    bool isIntendedDriverLoaded = false;
+    const bool doNotSend = mGpuStats.appPackageName.empty();
     if (api == GraphicsEnv::Api::API_GL) {
-        driver = mGpuStats.glDriverToLoad;
-        isIntendedDriverLoaded =
-                isLoaded && (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE);
+        if (doNotSend) mGpuStats.glDriverToSend = true;
+        mGpuStats.glDriverLoadingTime = driverLoadingTime;
     } else {
-        driver = mGpuStats.vkDriverToLoad;
-        isIntendedDriverLoaded =
-                isLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
+        if (doNotSend) mGpuStats.vkDriverToSend = true;
+        mGpuStats.vkDriverLoadingTime = driverLoadingTime;
     }
 
-    sendGpuStatsLocked(driver, isIntendedDriverLoaded, driverLoadingTime);
+    sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
 }
 
 static sp<IGpuService> getGpuService() {
@@ -249,7 +263,7 @@
     return interface_cast<IGpuService>(binder);
 }
 
-void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Driver driver, bool isDriverLoaded,
+void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Api api, bool isDriverLoaded,
                                      int64_t driverLoadingTime) {
     ATRACE_CALL();
 
@@ -263,19 +277,31 @@
           "\tdriverBuildTime[%" PRId64 "]\n"
           "\tappPackageName[%s]\n"
           "\tvulkanVersion[%d]\n"
-          "\tdriver[%d]\n"
+          "\tapi[%d]\n"
           "\tisDriverLoaded[%d]\n"
           "\tdriverLoadingTime[%" PRId64 "]",
           mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
           mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
-          mGpuStats.vulkanVersion, static_cast<int32_t>(driver), isDriverLoaded, driverLoadingTime);
+          mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
+
+    GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
+    bool isIntendedDriverLoaded = false;
+    if (api == GraphicsEnv::Api::API_GL) {
+        driver = mGpuStats.glDriverToLoad;
+        isIntendedDriverLoaded =
+                isDriverLoaded && (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE);
+    } else {
+        driver = mGpuStats.vkDriverToLoad;
+        isIntendedDriverLoaded =
+                isDriverLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
+    }
 
     const sp<IGpuService> gpuService = getGpuService();
     if (gpuService) {
         gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
                                 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
                                 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
-                                isDriverLoaded, driverLoadingTime);
+                                isIntendedDriverLoaded, driverLoadingTime);
     }
 }