1 // 2 // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 // main.cpp: DLL entry point and management of thread-local data. 8 9 #include "libEGL/main.h" 10 11 #include "common/debug.h" 12 13 static DWORD currentTLS = TLS_OUT_OF_INDEXES; 14 15 extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) 16 { 17 switch (reason) 18 { 19 case DLL_PROCESS_ATTACH: 20 { 21 #if !defined(ANGLE_DISABLE_TRACE) 22 FILE *debug = fopen(TRACE_OUTPUT_FILE, "rt"); 23 24 if (debug) 25 { 26 fclose(debug); 27 debug = fopen(TRACE_OUTPUT_FILE, "wt"); // Erase 28 29 if (debug) 30 { 31 fclose(debug); 32 } 33 } 34 #endif 35 36 currentTLS = TlsAlloc(); 37 38 if (currentTLS == TLS_OUT_OF_INDEXES) 39 { 40 return FALSE; 41 } 42 } 43 // Fall throught to initialize index 44 case DLL_THREAD_ATTACH: 45 { 46 egl::Current *current = (egl::Current*)LocalAlloc(LPTR, sizeof(egl::Current)); 47 48 if (current) 49 { 50 TlsSetValue(currentTLS, current); 51 52 current->error = EGL_SUCCESS; 53 current->API = EGL_OPENGL_ES_API; 54 current->display = EGL_NO_DISPLAY; 55 current->drawSurface = EGL_NO_SURFACE; 56 current->readSurface = EGL_NO_SURFACE; 57 } 58 } 59 break; 60 case DLL_THREAD_DETACH: 61 { 62 void *current = TlsGetValue(currentTLS); 63 64 if (current) 65 { 66 LocalFree((HLOCAL)current); 67 } 68 } 69 break; 70 case DLL_PROCESS_DETACH: 71 { 72 void *current = TlsGetValue(currentTLS); 73 74 if (current) 75 { 76 LocalFree((HLOCAL)current); 77 } 78 79 TlsFree(currentTLS); 80 } 81 break; 82 default: 83 break; 84 } 85 86 return TRUE; 87 } 88 89 namespace egl 90 { 91 void setCurrentError(EGLint error) 92 { 93 Current *current = (Current*)TlsGetValue(currentTLS); 94 95 current->error = error; 96 } 97 98 EGLint getCurrentError() 99 { 100 Current *current = (Current*)TlsGetValue(currentTLS); 101 102 return current->error; 103 } 104 105 void setCurrentAPI(EGLenum API) 106 { 107 Current *current = (Current*)TlsGetValue(currentTLS); 108 109 current->API = API; 110 } 111 112 EGLenum getCurrentAPI() 113 { 114 Current *current = (Current*)TlsGetValue(currentTLS); 115 116 return current->API; 117 } 118 119 void setCurrentDisplay(EGLDisplay dpy) 120 { 121 Current *current = (Current*)TlsGetValue(currentTLS); 122 123 current->display = dpy; 124 } 125 126 EGLDisplay getCurrentDisplay() 127 { 128 Current *current = (Current*)TlsGetValue(currentTLS); 129 130 return current->display; 131 } 132 133 void setCurrentDrawSurface(EGLSurface surface) 134 { 135 Current *current = (Current*)TlsGetValue(currentTLS); 136 137 current->drawSurface = surface; 138 } 139 140 EGLSurface getCurrentDrawSurface() 141 { 142 Current *current = (Current*)TlsGetValue(currentTLS); 143 144 return current->drawSurface; 145 } 146 147 void setCurrentReadSurface(EGLSurface surface) 148 { 149 Current *current = (Current*)TlsGetValue(currentTLS); 150 151 current->readSurface = surface; 152 } 153 154 EGLSurface getCurrentReadSurface() 155 { 156 Current *current = (Current*)TlsGetValue(currentTLS); 157 158 return current->readSurface; 159 } 160 161 void error(EGLint errorCode) 162 { 163 egl::setCurrentError(errorCode); 164 } 165 166 } 167