Exit HostConnection for thread which only uses gralloc

For certain guest process which maintains a thread pool and only calls gralloc
library, we should exit the host connection to avoid spawning too many
render threads on host emulator.

Test: run empty test activity for 3000 times and the number of render
thread on host should stay roughly the same.
BUG: 80050618

Change-Id: Idb282aa67138572519edee761f8bc6cb50c1653d
Signed-off-by: Weilun Du <wdu@google.com>
(cherry picked from commit 904b90eb097609762c7c35c321f82b2cc5245190)
diff --git a/system/gralloc/gralloc.cpp b/system/gralloc/gralloc.cpp
index fc68f4e..c4f5b73 100644
--- a/system/gralloc/gralloc.cpp
+++ b/system/gralloc/gralloc.cpp
@@ -339,6 +339,13 @@
         return -EIO; \
     }
 
+#define EXIT_GRALLOCONLY_HOST_CONNECTION \
+    if (hostCon && hostCon->isGrallocOnly()) { \
+        ALOGD("%s: exiting HostConnection (is buffer-handling thread)", \
+              __FUNCTION__); \
+        HostConnection::exit(); \
+    }
+
 #if PLATFORM_SDK_VERSION < 18
 // On older APIs, just define it as a value no one is going to use.
 #define HAL_PIXEL_FORMAT_YCbCr_420_888 0xFFFFFFFF
@@ -1046,6 +1053,8 @@
                 }
             }
         }
+        EXIT_GRALLOCONLY_HOST_CONNECTION;
+
     }
 
     //
@@ -1070,6 +1079,7 @@
         cb->ashmemBase = 0;
         cb->mappedPid = 0;
         D("%s: Unregister buffer previous mapped to pid %d", __FUNCTION__, getpid());
+        EXIT_GRALLOCONLY_HOST_CONNECTION;
     }
 
 done: