blob: ad1156309c40e0c9c718372727009e8f3b995d89 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00009#include "libGLESv2/main.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000010
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000011#include "common/debug.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000012#include "libEGL/Surface.h"
13
14#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015
16static DWORD currentTLS = TLS_OUT_OF_INDEXES;
17
18BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
19{
20 switch (reason)
21 {
22 case DLL_PROCESS_ATTACH:
23 {
24 currentTLS = TlsAlloc();
25
26 if (currentTLS == TLS_OUT_OF_INDEXES)
27 {
28 return FALSE;
29 }
30 }
31 // Fall throught to initialize index
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000032 case DLL_THREAD_ATTACH:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033 {
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000034 gl::Current *current = (gl::Current*)LocalAlloc(LPTR, sizeof(gl::Current));
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036 if (current)
37 {
38 TlsSetValue(currentTLS, current);
39
40 current->context = NULL;
41 current->display = NULL;
42 }
43 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000044 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045 case DLL_THREAD_DETACH:
46 {
47 void *current = TlsGetValue(currentTLS);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000048
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049 if (current)
50 {
51 LocalFree((HLOCAL)current);
52 }
53 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000054 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055 case DLL_PROCESS_DETACH:
56 {
57 void *current = TlsGetValue(currentTLS);
58
59 if (current)
60 {
61 LocalFree((HLOCAL)current);
62 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000063
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000064 TlsFree(currentTLS);
65 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000066 break;
67 default:
68 break;
69 }
70
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000071 return TRUE;
72}
73
74namespace gl
75{
76void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface)
77{
78 Current *current = (Current*)TlsGetValue(currentTLS);
79
80 current->context = context;
81 current->display = display;
82
83 if (context && display && surface)
84 {
85 context->makeCurrent(display, surface);
86 }
87}
88
89Context *getContext()
90{
91 Current *current = (Current*)TlsGetValue(currentTLS);
92
93 return current->context;
94}
95
daniel@transgaming.comae072af2010-05-05 18:47:28 +000096egl::Display *getDisplay()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097{
98 Current *current = (Current*)TlsGetValue(currentTLS);
daniel@transgaming.comae072af2010-05-05 18:47:28 +000099
100 return current->display;
101}
102
103IDirect3DDevice9 *getDevice()
104{
105 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
107 return display->getDevice();
108}
109}
110
111// Records an error code
112void error(GLenum errorCode)
113{
114 gl::Context *context = glGetCurrentContext();
115
116 if (context)
117 {
118 switch (errorCode)
119 {
120 case GL_INVALID_ENUM:
121 context->recordInvalidEnum();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000122 TRACE("\t! Error generated: invalid enum\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 break;
124 case GL_INVALID_VALUE:
125 context->recordInvalidValue();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000126 TRACE("\t! Error generated: invalid value\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127 break;
128 case GL_INVALID_OPERATION:
129 context->recordInvalidOperation();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000130 TRACE("\t! Error generated: invalid operation\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 break;
132 case GL_OUT_OF_MEMORY:
133 context->recordOutOfMemory();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000134 TRACE("\t! Error generated: out of memory\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 break;
136 case GL_INVALID_FRAMEBUFFER_OPERATION:
137 context->recordInvalidFramebufferOperation();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000138 TRACE("\t! Error generated: invalid framebuffer operation\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 break;
140 default: UNREACHABLE();
141 }
142 }
143}