emulator: opengl: Fix Mac build

This fixes several issues when building the host libraris on Darwin.
Note that there is still not proper implementation of backend functions.

Change-Id: I3ba4120df6545a8c2aa62fdfcaadaf5ff4972456
diff --git a/tools/emulator/opengl/host/libs/Translator/EGL/Android.mk b/tools/emulator/opengl/host/libs/Translator/EGL/Android.mk
index 9d6321d..8f7601b 100644
--- a/tools/emulator/opengl/host/libs/Translator/EGL/Android.mk
+++ b/tools/emulator/opengl/host/libs/Translator/EGL/Android.mk
@@ -10,7 +10,7 @@
 
 ifeq ($(HOST_OS),linux)
     OS_SRCS = EglX11Api.cpp
-    LOCAL_LDLIBS := -lX11 -lGL -ldl
+    LOCAL_LDLIBS := -lX11 -lGL -ldl -lpthread
 endif
 
 ifeq ($(HOST_OS),darwin)
diff --git a/tools/emulator/opengl/host/libs/Translator/EGL/EglImp.cpp b/tools/emulator/opengl/host/libs/Translator/EGL/EglImp.cpp
index e45000a..5895e42 100644
--- a/tools/emulator/opengl/host/libs/Translator/EGL/EglImp.cpp
+++ b/tools/emulator/opengl/host/libs/Translator/EGL/EglImp.cpp
@@ -34,14 +34,15 @@
 #define MINOR          1
 #define MAJOR          4
 
-//declerations
+//declarations
+
 EglImage *attachEGLImage(unsigned int imageId);
 void detachEGLImage(unsigned int imageId);
 
+#define tls_thread  EglThreadInfo::get()
 
 EglGlobalInfo* g_eglInfo = EglGlobalInfo::getInstance();
 
-__thread EglThreadInfo*    tls_thread  = NULL;
 static EGLiface            s_eglIface = {
     getThreadInfo    : getThreadInfo,      // implemented in ThreadInfo.cpp
     eglAttachEGLImage:attachEGLImage,
@@ -71,10 +72,7 @@
 /****************************************************************************************************************************************/
 //macros for accessing global egl info & tls objects
 
-#define CURRENT_THREAD()                                     \
-        if(!tls_thread) {                                    \
-            tls_thread = new EglThreadInfo();                \
-        }
+#define CURRENT_THREAD() do {} while (0);
 
 #define RETURN_ERROR(ret,err)                                \
         CURRENT_THREAD()                                     \
diff --git a/tools/emulator/opengl/host/libs/Translator/EGL/EglMacApi.cpp b/tools/emulator/opengl/host/libs/Translator/EGL/EglMacApi.cpp
index e7f92ba..327588c 100644
--- a/tools/emulator/opengl/host/libs/Translator/EGL/EglMacApi.cpp
+++ b/tools/emulator/opengl/host/libs/Translator/EGL/EglMacApi.cpp
@@ -20,7 +20,7 @@
 //TODO: implementation for mac for all funcs
 namespace EglOS {
 
-EGLNativeDisplayType getDefaultDisplay() {return NULL}
+EGLNativeDisplayType getDefaultDisplay() {return NULL;}
 
 bool releaseDisplay(EGLNativeDisplayType dpy) {
     return false;
@@ -53,6 +53,10 @@
     return NULL;
 }
 
+bool releasePbuffer(EGLNativeDisplayType dis,EGLNativePbufferType pb) {
+    return true;
+}
+
 EGLNativeContextType createContext(EGLNativeDisplayType dpy,EglConfig* cfg,EGLNativeContextType sharedContext) {
  return NULL;
 }
diff --git a/tools/emulator/opengl/host/libs/Translator/EGL/EglOsApi.h b/tools/emulator/opengl/host/libs/Translator/EGL/EglOsApi.h
index 6bd2b76..f72dfd6 100644
--- a/tools/emulator/opengl/host/libs/Translator/EGL/EglOsApi.h
+++ b/tools/emulator/opengl/host/libs/Translator/EGL/EglOsApi.h
@@ -17,7 +17,11 @@
 #define EGL_OS_API_H
 
 #include <EGL/egl.h>
+#ifdef __APPLE__
+#include <OpenGL/gl.h>
+#else
 #include <GL/gl.h>
+#endif
 #include "EglConfig.h"
 #include "EglDisplay.h"
 #include "EglPbufferSurface.h"
diff --git a/tools/emulator/opengl/host/libs/Translator/EGL/EglThreadInfo.cpp b/tools/emulator/opengl/host/libs/Translator/EGL/EglThreadInfo.cpp
index a9bfc56..1b403f2 100644
--- a/tools/emulator/opengl/host/libs/Translator/EGL/EglThreadInfo.cpp
+++ b/tools/emulator/opengl/host/libs/Translator/EGL/EglThreadInfo.cpp
@@ -17,3 +17,26 @@
 #include "EglOsApi.h"
 
 EglThreadInfo::EglThreadInfo():m_err(EGL_SUCCESS),m_api(EGL_OPENGL_ES_API) {}
+
+#include <cutils/threads.h>
+
+static thread_store_t s_tls = THREAD_STORE_INITIALIZER;
+
+static void tlsDestruct(void *ptr)
+{
+    if (ptr) {
+        EglThreadInfo *ti = (EglThreadInfo *)ptr;
+        delete ti;
+    }
+}
+
+EglThreadInfo* EglThreadInfo::get(void)
+{
+    EglThreadInfo *ti = (EglThreadInfo *)thread_store_get(&s_tls);
+    if (!ti) {
+        ti = new EglThreadInfo();
+        thread_store_set(&s_tls, ti, tlsDestruct);
+    }
+    return ti;
+}
+
diff --git a/tools/emulator/opengl/host/libs/Translator/EGL/EglThreadInfo.h b/tools/emulator/opengl/host/libs/Translator/EGL/EglThreadInfo.h
index c811508..9d2df10 100644
--- a/tools/emulator/opengl/host/libs/Translator/EGL/EglThreadInfo.h
+++ b/tools/emulator/opengl/host/libs/Translator/EGL/EglThreadInfo.h
@@ -32,6 +32,8 @@
     void       setApi(EGLenum api){m_api = api;}
     EGLenum    getApi(){return m_api;}
 
+    static EglThreadInfo*  get(void) __attribute__((const));
+
 private:
     EglDisplay*     m_currentDisplay;
     EGLint          m_err;
diff --git a/tools/emulator/opengl/host/libs/Translator/EGL/ThreadInfo.cpp b/tools/emulator/opengl/host/libs/Translator/EGL/ThreadInfo.cpp
index ed2880f..6814fe9 100644
--- a/tools/emulator/opengl/host/libs/Translator/EGL/ThreadInfo.cpp
+++ b/tools/emulator/opengl/host/libs/Translator/EGL/ThreadInfo.cpp
@@ -16,7 +16,6 @@
 
 #include <GLcommon/ThreadInfo.h>
 
-__thread ThreadInfo* thread  = NULL;
 
 void ThreadInfo::updateInfo(void* eglCtx,void* dpy,void* glesCtx,ShareGroupPtr share,ObjectNameManager* manager) {
     eglContext  = eglCtx;
@@ -27,6 +26,9 @@
 }
 
 #ifdef __linux__
+
+__thread ThreadInfo* thread  = NULL;
+
 ThreadInfo* getThreadInfo(){
     if(!thread) {
         thread = new ThreadInfo();
@@ -35,6 +37,7 @@
 }
 
 #else
+
 #include <cutils/threads.h>
 static thread_store_t s_tls = THREAD_STORE_INITIALIZER;
 
@@ -46,13 +49,14 @@
     }
 }
 
-RenderThreadInfo *getRenderThreadInfo()
+ThreadInfo *getThreadInfo()
 {
     ThreadInfo *ti = (ThreadInfo *)thread_store_get(&s_tls);
     if (!ti) {
-        ti = new RenderThreadInfo();
+        ti = new ThreadInfo();
         thread_store_set(&s_tls, ti, tlsDestruct);
     }
     return ti;
 }
+
 #endif
diff --git a/tools/emulator/opengl/host/libs/Translator/include/EGL/eglplatform.h b/tools/emulator/opengl/host/libs/Translator/include/EGL/eglplatform.h
index 8ee5634..ec70a0d 100644
--- a/tools/emulator/opengl/host/libs/Translator/include/EGL/eglplatform.h
+++ b/tools/emulator/opengl/host/libs/Translator/include/EGL/eglplatform.h
@@ -73,6 +73,16 @@
 typedef HBITMAP               EGLNativePixmapType;
 typedef HWND                  EGLNativeWindowType;
 
+#elif defined(__APPLE__)
+
+typedef int  EGLNativePixelFormatType;
+
+typedef struct _EGLNativeContextType*      EGLNativeContextType;
+typedef struct _EGLNativePbufferType*      EGLNativePbufferType;
+typedef struct _EGLNativeDisplayType*      EGLNativeDisplayType;
+typedef int       EGLNativePixmapType;
+typedef int       EGLNativeWindowType;
+
 #elif defined(__unix__)
 
 /* X11 (tentative)  */