Use a per-process server address for the GLES server

Previously we used a hardcoded address (tcp port, unix pipe path,
etc.) for the OpenGLRender system. Multiple emulators would all try to
listen on the same address, with the system non-deterministically (?)
choosing which one accepted each new connection. This resulted in
frames going to the wrong emulator window, one emulator shutting down
another's OpenGL system, etc.

Now the OpenGLRender server requests an unused tcp port or derives a
path from the pid, and reports the address back to the emulator client
to use for future connections from the guest.

Change-Id: I139d32615200b36b87f2d2ede4abb4060ec02776
diff --git a/android/hw-pipe-net.c b/android/hw-pipe-net.c
index 3e8dc70..1c71f18 100644
--- a/android/hw-pipe-net.c
+++ b/android/hw-pipe-net.c
@@ -492,19 +492,18 @@
         return NULL;
     }
 
+    char server_addr[PATH_MAX];
+    android_gles_server_path(server_addr, sizeof(server_addr));
 #ifndef _WIN32
     if (android_gles_fast_pipes) {
-        char  unix_path[PATH_MAX];
-        android_gles_unix_path(unix_path, sizeof(unix_path), ANDROID_OPENGLES_BASE_PORT);
-        pipe = (NetPipe *)netPipe_initUnix(hwpipe, _looper, unix_path);
-        D("Creating Unix OpenGLES pipe for GPU emulation: %s", unix_path);
+        pipe = (NetPipe *)netPipe_initUnix(hwpipe, _looper, server_addr);
+        D("Creating Unix OpenGLES pipe for GPU emulation: %s", server_addr);
     } else {
 #else /* _WIN32 */
     {
 #endif
         /* Connect through TCP as a fallback */
-        snprintf(temp, sizeof temp, "%d", ANDROID_OPENGLES_BASE_PORT);
-        pipe = (NetPipe *)netPipe_initTcp(hwpipe, _looper, temp);
+        pipe = (NetPipe *)netPipe_initTcp(hwpipe, _looper, server_addr);
         D("Creating TCP OpenGLES pipe for GPU emulation!");
     }
     if (pipe != NULL) {
diff --git a/android/opengles.c b/android/opengles.c
index 7405750..1fa6421 100644
--- a/android/opengles.c
+++ b/android/opengles.c
@@ -61,6 +61,7 @@
 
 static ADynamicLibrary*  rendererLib;
 static int               rendererStarted;
+static char              rendererAddress[256];
 
 /* Define the function pointers */
 #define DYNLINK_FUNC(name) \
@@ -152,7 +153,7 @@
         return 0;
     }
 
-    if (!initOpenGLRenderer(width, height, ANDROID_OPENGLES_BASE_PORT)) {
+    if (!initOpenGLRenderer(width, height, rendererAddress, sizeof(rendererAddress))) {
         D("Can't start OpenGLES renderer?");
         return -1;
     }
@@ -177,7 +178,6 @@
 
 static void extractBaseString(char* dst, const char* src, size_t dstSize)
 {
-    size_t len = strlen(src);
     const char* begin = strchr(src, '(');
     const char* end = strrchr(src, ')');
 
@@ -211,7 +211,7 @@
 
     if (!rendererStarted) {
         D("Can't get OpenGL ES hardware strings when renderer not started");
-        vendor[0] = renderer[0] = version = '\0';
+        vendor[0] = renderer[0] = version[0] = '\0';
         return;
     }
 
@@ -274,18 +274,9 @@
 }
 
 void
-android_gles_unix_path(char* buff, size_t buffsize, int port)
+android_gles_server_path(char* buff, size_t buffsize)
 {
-    const char* user = getenv("USER");
-    char *p = buff, *end = buff + buffsize;
-
-    /* The logic here must correspond to the one inside
-     * development/tools/emulator/opengl/shared/libOpenglCodecCommon/UnixStream.cpp */
-    p = bufprint(p, end, "/tmp/");
-    if (user && user[0]) {
-        p = bufprint(p, end, "android-%s/", user);
-    }
-    p = bufprint(p, end, "qemu-gles-%d", port);
+    strncpy_safe(buff, rendererAddress, buffsize);
 }
 
 #else // CONFIG_ANDROID_OPENGLES
@@ -330,7 +321,7 @@
 void android_redrawOpenglesWindow(void)
 {}
 
-void android_gles_unix_path(char* buff, size_t buffsize, int port)
+void android_gles_server_path(char* buff, size_t buffsize)
 {
     buff[0] = '\0';
 }
diff --git a/android/opengles.h b/android/opengles.h
index aac6249..6330287 100644
--- a/android/opengles.h
+++ b/android/opengles.h
@@ -14,8 +14,6 @@
 
 #include <stddef.h>
 
-#define ANDROID_OPENGLES_BASE_PORT  22468
-
 /* Call this function to initialize the hardware opengles emulation.
  * This function will abort if we can't find the corresponding host
  * libraries through dlopen() or equivalent.
@@ -59,8 +57,10 @@
  */
 extern int  android_gles_fast_pipes;
 
-/* Write the path of the Unix socket we're going to use to access GLES on a given <port> */
-/* The result is only valid on Unix systems */
-void android_gles_unix_path(char* buff, size_t buffsize, int port);
+/* Get the address of the socket that clients should connect to to access GLES.
+ * For TCP this is just the port number (as a string) on the loopback address.
+ * For UNIX and Win32 pipes it is the full pathname of the pipe.
+ */
+void android_gles_server_path(char* buff, size_t buffsize);
 
 #endif /* ANDROID_OPENGLES_H */