blob: 83ea8fe67a429b96b503080507a5c41132fd7932 [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.com78d44862011-11-09 17:46:33 +000010#include "libGLESv2/utilities.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000012#include "common/debug.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000013#include "libEGL/Surface.h"
14
15#include "libGLESv2/Framebuffer.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016
17static DWORD currentTLS = TLS_OUT_OF_INDEXES;
18
19BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
20{
21 switch (reason)
22 {
23 case DLL_PROCESS_ATTACH:
24 {
25 currentTLS = TlsAlloc();
26
27 if (currentTLS == TLS_OUT_OF_INDEXES)
28 {
29 return FALSE;
30 }
31 }
32 // Fall throught to initialize index
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000033 case DLL_THREAD_ATTACH:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000034 {
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000035 gl::Current *current = (gl::Current*)LocalAlloc(LPTR, sizeof(gl::Current));
36
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037 if (current)
38 {
39 TlsSetValue(currentTLS, current);
40
41 current->context = NULL;
42 current->display = NULL;
43 }
44 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000045 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046 case DLL_THREAD_DETACH:
47 {
48 void *current = TlsGetValue(currentTLS);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000049
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050 if (current)
51 {
52 LocalFree((HLOCAL)current);
53 }
54 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000055 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000056 case DLL_PROCESS_DETACH:
57 {
58 void *current = TlsGetValue(currentTLS);
59
60 if (current)
61 {
62 LocalFree((HLOCAL)current);
63 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000064
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065 TlsFree(currentTLS);
66 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +000067 break;
68 default:
69 break;
70 }
71
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072 return TRUE;
73}
74
75namespace gl
76{
77void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface)
78{
79 Current *current = (Current*)TlsGetValue(currentTLS);
80
81 current->context = context;
82 current->display = display;
83
84 if (context && display && surface)
85 {
86 context->makeCurrent(display, surface);
87 }
88}
89
90Context *getContext()
91{
92 Current *current = (Current*)TlsGetValue(currentTLS);
93
94 return current->context;
95}
96
daniel@transgaming.comae072af2010-05-05 18:47:28 +000097egl::Display *getDisplay()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098{
99 Current *current = (Current*)TlsGetValue(currentTLS);
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000100
101 return current->display;
102}
103
104IDirect3DDevice9 *getDevice()
105{
106 egl::Display *display = getDisplay();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
108 return display->getDevice();
109}
daniel@transgaming.com78d44862011-11-09 17:46:33 +0000110
111bool checkDeviceLost(HRESULT errorCode)
112{
113 egl::Display *display = NULL;
114
115 if (isDeviceLostError(errorCode))
116 {
117 return true;
118 }
119 return false;
120}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121}
122
123// Records an error code
124void error(GLenum errorCode)
125{
126 gl::Context *context = glGetCurrentContext();
127
128 if (context)
129 {
130 switch (errorCode)
131 {
132 case GL_INVALID_ENUM:
133 context->recordInvalidEnum();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000134 TRACE("\t! Error generated: invalid enum\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135 break;
136 case GL_INVALID_VALUE:
137 context->recordInvalidValue();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000138 TRACE("\t! Error generated: invalid value\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 break;
140 case GL_INVALID_OPERATION:
141 context->recordInvalidOperation();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000142 TRACE("\t! Error generated: invalid operation\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143 break;
144 case GL_OUT_OF_MEMORY:
145 context->recordOutOfMemory();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000146 TRACE("\t! Error generated: out of memory\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147 break;
148 case GL_INVALID_FRAMEBUFFER_OPERATION:
149 context->recordInvalidFramebufferOperation();
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000150 TRACE("\t! Error generated: invalid framebuffer operation\n");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 break;
152 default: UNREACHABLE();
153 }
154 }
155}