single host connection per gralloc process

reduce jank

Change-Id: I158dbf5e43e9413c40332c831409431f7f35ae42
diff --git a/system/gralloc/gralloc.cpp b/system/gralloc/gralloc.cpp
index 48b4079..2a5a174 100644
--- a/system/gralloc/gralloc.cpp
+++ b/system/gralloc/gralloc.cpp
@@ -331,12 +331,21 @@
     return 0;
 }
 
+static HostConnection* sHostCon = NULL;
+
+static HostConnection* createOrGetHostConnection() {
+    if (!sHostCon) {
+        sHostCon = HostConnection::createUnique();
+    }
+    return sHostCon;
+}
+
 #define DEFINE_HOST_CONNECTION \
-    HostConnection *hostCon = HostConnection::get(); \
+    HostConnection *hostCon = createOrGetHostConnection(); \
     ExtendedRCEncoderContext *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL)
 
 #define DEFINE_AND_VALIDATE_HOST_CONNECTION \
-    HostConnection *hostCon = HostConnection::get(); \
+    HostConnection *hostCon = createOrGetHostConnection(); \
     if (!hostCon) { \
         ALOGE("gralloc: Failed to get host connection\n"); \
         return -EIO; \
@@ -351,7 +360,6 @@
     if (hostCon && hostCon->isGrallocOnly()) { \
         ALOGD("%s: exiting HostConnection (is buffer-handling thread)", \
               __FUNCTION__); \
-        HostConnection::exit(); \
     }
 
 #if PLATFORM_SDK_VERSION < 18
@@ -719,6 +727,8 @@
 
         cb->setFd(fd);
 
+        hostCon->lock();
+
         if (rcEnc->getDmaVersion() > 0) {
             D("%s: creating goldfish dma region of size %lu (cb fd %d)\n", __FUNCTION__, ashmem_size, cb->fd);
             init_gralloc_dmaregion();
@@ -726,6 +736,7 @@
         } else {
             cb->goldfish_dma.fd = -1;
         }
+        hostCon->unlock();
     } else {
         cb->goldfish_dma.fd = -1;
     }
@@ -741,11 +752,13 @@
             if (HAL_PIXEL_FORMAT_RGBX_8888 == format) {
                 allocFormat = GL_RGB;
             }
+            hostCon->lock();
             if (s_grdma) {
                 cb->hostHandle = rcEnc->rcCreateColorBufferDMA(rcEnc, w, h, allocFormat, cb->emuFrameworkFormat);
             } else {
                 cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, allocFormat);
             }
+            hostCon->unlock();
         }
 
         if (!cb->hostHandle) {
@@ -817,7 +830,9 @@
         if (*openCountPtr > 0) {
             DEFINE_AND_VALIDATE_HOST_CONNECTION;
             D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
+            hostCon->lock();
             rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
+            hostCon->unlock();
         } else {
             D("A rcCloseColorBuffer is owed!!! sdk ver: %d", PLATFORM_SDK_VERSION);
             *openCountPtr = -1;
@@ -916,8 +931,10 @@
     (*postCountPtr)++;
 
     // send post request to host
+    hostCon->lock();
     rcEnc->rcFBPost(rcEnc, cb->hostHandle);
     hostCon->flush();
+    hostCon->unlock();
 
     return 0;
 }
@@ -959,8 +976,10 @@
     DEFINE_AND_VALIDATE_HOST_CONNECTION;
 
     // send request to host
+    hostCon->lock();
     rcEnc->rcFBSetSwapInterval(rcEnc, interval);
     hostCon->flush();
+    hostCon->unlock();
 
     return 0;
 }
@@ -1002,7 +1021,9 @@
     if (cb->hostHandle != 0 && !cb->hasRefcountPipe()) {
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
         D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
+        hostCon->lock();
         rcEnc->rcOpenColorBuffer2(rcEnc, cb->hostHandle);
+        hostCon->unlock();
     }
 
     //
@@ -1024,10 +1045,12 @@
         }
 
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
+        hostCon->lock();
         if (rcEnc->getDmaVersion() > 0) {
             init_gralloc_dmaregion();
             gralloc_dmaregion_register_ashmem(cb->ashmemSize);
         }
+        hostCon->unlock();
 
     }
 
@@ -1058,6 +1081,7 @@
     if (cb->hostHandle && !cb->hasRefcountPipe()) {
         D("Closing host ColorBuffer 0x%x\n", cb->hostHandle);
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
+        hostCon->lock();
         rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle);
 
         if (isHidlGralloc) {
@@ -1072,6 +1096,7 @@
                 }
             }
         }
+        hostCon->unlock();
         EXIT_GRALLOCONLY_HOST_CONNECTION;
 
     }
@@ -1087,7 +1112,6 @@
 
         if (!SHOULD_UNMAP) goto done;
 
-        DEFINE_AND_VALIDATE_HOST_CONNECTION;
 
         void *vaddr;
         int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
@@ -1098,7 +1122,6 @@
         cb->ashmemBase = 0;
         cb->mappedPid = 0;
         D("%s: Unregister buffer previous mapped to pid %d", __FUNCTION__, getpid());
-        EXIT_GRALLOCONLY_HOST_CONNECTION;
     }
 
 done:
@@ -1191,6 +1214,7 @@
     if (cb->hostHandle) {
         // Make sure we have host connection
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
+        hostCon->lock();
 
         //
         // flush color buffer write cache on host and get its sync status.
@@ -1228,6 +1252,7 @@
                 delete [] tmpBuf;
             }
         }
+        hostCon->unlock();
     }
 
     //
@@ -1276,6 +1301,7 @@
 
         // Make sure we have host connection
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
+        hostCon->lock();
 
         void *cpu_addr = (void *)(cb->ashmemBase + getAshmemColorOffset(cb));
 
@@ -1287,6 +1313,7 @@
             updateHostColorBuffer(cb, false, rgb_addr);
         }
 
+        hostCon->unlock();
         DD("gralloc_unlock success. cpu_addr: %p", cpu_addr);
     }
 
@@ -1423,8 +1450,8 @@
 
         // Create host connection and keep it in the TLS.
         // return error if connection with host can not be established
-        HostConnection *hostCon = HostConnection::get();
-        if (!hostCon) {
+        HostConnection *hostConn = createOrGetHostConnection();
+        if (!hostConn) {
             ALOGE("gralloc: failed to get host connection while opening %s\n", name);
             return -EIO;
         }
@@ -1458,6 +1485,7 @@
 
         // return error if connection with host can not be established
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
+        hostCon->lock();
 
         //
         // Query the host for Framebuffer attributes
@@ -1477,6 +1505,7 @@
         D("gralloc: min_swap=%d\n", min_si);
         EGLint max_si = rcEnc->rcGetFBParam(rcEnc, FB_MAX_SWAP_INTERVAL);
         D("gralloc: max_swap=%d\n", max_si);
+        hostCon->unlock();
 
         //
         // Allocate memory for the framebuffer device