2010-08-02 Paul Sawaya <psawaya@apple.com>
[WebKit-https.git] / ANGLE / src / libEGL / main.cpp
1 //
2 // Copyright (c) 2002-2010 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 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
16 {
17     switch (reason)
18     {
19       case DLL_PROCESS_ATTACH:
20         {
21             #ifndef NDEBUG
22                 FILE *debug = fopen("debug.txt", "rt");
23
24                 if (debug)
25                 {
26                     fclose(debug);
27                     debug = fopen("debug.txt", "wt");   // Erase
28                     fclose(debug);
29                 }
30             #endif
31
32             currentTLS = TlsAlloc();
33
34             if (currentTLS == TLS_OUT_OF_INDEXES)
35             {
36                 return FALSE;
37             }
38         }
39         // Fall throught to initialize index
40       case DLL_THREAD_ATTACH:
41         {
42             egl::Current *current = (egl::Current*)LocalAlloc(LPTR, sizeof(egl::Current));
43
44             if (current)
45             {
46                 TlsSetValue(currentTLS, current);
47
48                 current->error = EGL_SUCCESS;
49                 current->API = EGL_OPENGL_ES_API;
50                 current->display = EGL_NO_DISPLAY;
51                 current->drawSurface = EGL_NO_SURFACE;
52                 current->readSurface = EGL_NO_SURFACE;
53             }
54         }
55         break;
56       case DLL_THREAD_DETACH:
57         {
58             void *current = TlsGetValue(currentTLS);
59
60             if (current)
61             {
62                 LocalFree((HLOCAL)current);
63             }
64         }
65         break;
66       case DLL_PROCESS_DETACH:
67         {
68             void *current = TlsGetValue(currentTLS);
69
70             if (current)
71             {
72                 LocalFree((HLOCAL)current);
73             }
74
75             TlsFree(currentTLS);
76         }
77         break;
78       default:
79         break;
80     }
81
82     return TRUE;
83 }
84
85 namespace egl
86 {
87 void setCurrentError(EGLint error)
88 {
89     Current *current = (Current*)TlsGetValue(currentTLS);
90
91     current->error = error;
92 }
93
94 EGLint getCurrentError()
95 {
96     Current *current = (Current*)TlsGetValue(currentTLS);
97
98     return current->error;
99 }
100
101 void setCurrentAPI(EGLenum API)
102 {
103     Current *current = (Current*)TlsGetValue(currentTLS);
104
105     current->API = API;
106 }
107
108 EGLenum getCurrentAPI()
109 {
110     Current *current = (Current*)TlsGetValue(currentTLS);
111
112     return current->API;
113 }
114
115 void setCurrentDisplay(EGLDisplay dpy)
116 {
117     Current *current = (Current*)TlsGetValue(currentTLS);
118
119     current->display = dpy;
120 }
121
122 EGLDisplay getCurrentDisplay()
123 {
124     Current *current = (Current*)TlsGetValue(currentTLS);
125
126     return current->display;
127 }
128
129 void setCurrentDrawSurface(EGLSurface surface)
130 {
131     Current *current = (Current*)TlsGetValue(currentTLS);
132
133     current->drawSurface = surface;
134 }
135
136 EGLSurface getCurrentDrawSurface()
137 {
138     Current *current = (Current*)TlsGetValue(currentTLS);
139
140     return current->drawSurface;
141 }
142
143 void setCurrentReadSurface(EGLSurface surface)
144 {
145     Current *current = (Current*)TlsGetValue(currentTLS);
146
147     current->readSurface = surface;
148 }
149
150 EGLSurface getCurrentReadSurface()
151 {
152     Current *current = (Current*)TlsGetValue(currentTLS);
153
154     return current->readSurface;
155 }
156 }
157
158 void error(EGLint errorCode)
159 {
160     egl::setCurrentError(errorCode);
161 }