EGL translator : fix windows egl for handling device contexts

on eglCreateContext the translator created a window for the
specified config with a matching pixelformat and saved it's
device context for future use.
however when the thread exited the window was destroyed by windows
and the handle saved became invalid.
the next call to wglCreateContext with the handle saved failed.

fixed the problem by creating windows per thread and not in a
global pool.
each thread will create its own window for each config when asked
the window will be destroyed when the thread exits.

Change-Id: I63668a20e8c30c42c4cfcaca664938683600b28e
diff --git a/tools/emulator/opengl/host/libs/Translator/EGL/EglWindowsApi.cpp b/tools/emulator/opengl/host/libs/Translator/EGL/EglWindowsApi.cpp
index 76820d6..e446249 100644
--- a/tools/emulator/opengl/host/libs/Translator/EGL/EglWindowsApi.cpp
+++ b/tools/emulator/opengl/host/libs/Translator/EGL/EglWindowsApi.cpp
@@ -30,25 +30,40 @@
     bool isPixelFormatSet;
 };
 
+struct TlsData {
+    std::map<int,DisplayInfo> m_map;
+};
+
+static DWORD s_tlsIndex = 0;
+
+static TlsData *getTLS() {
+    TlsData *tls = (TlsData *)TlsGetValue(s_tlsIndex);
+    if (!tls) {
+        tls = new TlsData();
+        TlsSetValue(s_tlsIndex, tls);
+    }
+    return tls;
+}
+
 class WinDisplay{
 public:
      typedef enum {
                       DEFAULT_DISPLAY = 0
                   };
      WinDisplay(){};
-     DisplayInfo& getInfo(int configurationIndex){ return m_map[configurationIndex];}
-     HDC  getDC(int configId){return m_map[configId].dc;}
+     DisplayInfo& getInfo(int configurationIndex){ return getTLS()->m_map[configurationIndex];}
+     HDC  getDC(int configId){return getTLS()->m_map[configId].dc;}
      void setInfo(int configurationIndex,const DisplayInfo& info);
-     bool isPixelFormatSet(int cfgId){ return m_map[cfgId].isPixelFormatSet;}
-     void pixelFormatWasSet(int cfgId){m_map[cfgId].isPixelFormatSet = true;}
+     bool isPixelFormatSet(int cfgId){ return getTLS()->m_map[cfgId].isPixelFormatSet;}
+     void pixelFormatWasSet(int cfgId){getTLS()->m_map[cfgId].isPixelFormatSet = true;}
      bool infoExists(int configurationIndex);
      void releaseAll();
-private:
-    std::map<int,DisplayInfo> m_map;
 };
 
 void WinDisplay::releaseAll(){
-    for(std::map<int,DisplayInfo>::iterator it = m_map.begin(); it != m_map.end();it++){
+    TlsData * tls = getTLS();
+    
+    for(std::map<int,DisplayInfo>::iterator it = tls->m_map.begin(); it != tls->m_map.end();it++){
        if((*it).second.hwnd){
            DestroyWindow((*it).second.hwnd);
        }
@@ -57,11 +72,11 @@
 }
 
 bool WinDisplay::infoExists(int configurationIndex){
-    return m_map.find(configurationIndex) == m_map.end();
+    return getTLS()->m_map.find(configurationIndex) != getTLS()->m_map.end();
 }
 
 void WinDisplay::setInfo(int configurationIndex,const DisplayInfo& info){
-    m_map[configurationIndex] = info;
+    getTLS()->m_map[configurationIndex] = info;
 }
 
 struct WglExtProcs{
@@ -193,6 +208,7 @@
 }
 
 EGLNativeInternalDisplayType getDefaultDisplay() {
+    if (!s_tlsIndex) s_tlsIndex = TlsAlloc();
     WinDisplay* dpy = new WinDisplay();
 
     HWND hwnd = createDummyWindow();
@@ -202,6 +218,7 @@
 }
 
 EGLNativeInternalDisplayType getInternalDisplay(EGLNativeDisplayType display){
+    if (!s_tlsIndex) s_tlsIndex = TlsAlloc();
     WinDisplay* dpy = new WinDisplay();
     dpy->setInfo(WinDisplay::DEFAULT_DISPLAY,DisplayInfo(display,NULL));
     return dpy;
@@ -210,7 +227,7 @@
 static HDC getDummyDC(EGLNativeInternalDisplayType display,int cfgId){
 
     HDC dpy = NULL;
-    if(display->infoExists(cfgId)){
+    if(!display->infoExists(cfgId)){
         HWND hwnd = createDummyWindow();
         dpy  = GetDC(hwnd);
         display->setInfo(cfgId,DisplayInfo(dpy,hwnd));