blob: e351580a7a9cdbe7bf050d8d7b46d7180d2fbbdf [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001// SwiftShader Software Renderer
2//
John Baumand4ae8632014-05-06 16:18:33 -04003// Copyright(c) 2005-2013 TransGaming Inc.
John Bauman66b8ab22014-05-06 15:57:45 -04004//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12// main.cpp: DLL entry point and management of thread-local data.
13
14#include "main.h"
15
16#include "Framebuffer.h"
17#include "libEGL/Surface.h"
18#include "Common/Thread.hpp"
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040019#include "Common/SharedLibrary.hpp"
John Bauman66b8ab22014-05-06 15:57:45 -040020#include "common/debug.h"
21
John Bauman66b8ab22014-05-06 15:57:45 -040022#if !defined(_MSC_VER)
23#define CONSTRUCTOR __attribute__((constructor))
24#define DESTRUCTOR __attribute__((destructor))
25#else
26#define CONSTRUCTOR
27#define DESTRUCTOR
28#endif
29
30static void glAttachThread()
31{
32 TRACE("()");
John Bauman66b8ab22014-05-06 15:57:45 -040033}
34
35static void glDetachThread()
36{
37 TRACE("()");
John Bauman66b8ab22014-05-06 15:57:45 -040038}
39
40CONSTRUCTOR static bool glAttachProcess()
41{
42 TRACE("()");
43
John Bauman66b8ab22014-05-06 15:57:45 -040044 glAttachThread();
45
Nicolas Capense2540102014-11-05 09:34:52 -050046 #if defined(_WIN32)
47 const char *libEGL_lib[] = {"libEGL.dll", "libEGL_translator.dll"};
Nicolas Capens4cadfe32014-12-10 22:26:26 -050048 #elif defined(__LP64__)
49 const char *libEGL_lib[] = {"lib64EGL_translator.so", "libEGL.so.1", "libEGL.so"};
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040050 #else
Nicolas Capens8d869e02014-12-08 16:52:06 -050051 const char *libEGL_lib[] = {"libEGL_translator.so", "libEGL.so.1", "libEGL.so"};
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040052 #endif
53
54 libEGL = loadLibrary(libEGL_lib);
Nicolas Capens53e38862014-11-05 16:05:12 -050055 egl::getCurrentContext = (egl::Context *(*)())getProcAddress(libEGL, "clientGetCurrentContext");
56 egl::getCurrentDisplay = (egl::Display *(*)())getProcAddress(libEGL, "clientGetCurrentDisplay");
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040057
Nicolas Capensd76b5df2014-10-28 15:54:46 -040058 #if defined(_WIN32)
Nicolas Capense2540102014-11-05 09:34:52 -050059 const char *libGLES_CM_lib[] = {"libGLES_CM.dll", "libGLES_CM_translator.dll"};
Nicolas Capens4cadfe32014-12-10 22:26:26 -050060 #elif defined(__LP64__)
61 const char *libGLES_CM_lib[] = {"lib64GLES_CM_translator.so", "libGLES_CM.so.1", "libGLES_CM.so"};
Nicolas Capensd76b5df2014-10-28 15:54:46 -040062 #else
Nicolas Capens8d869e02014-12-08 16:52:06 -050063 const char *libGLES_CM_lib[] = {"libGLES_CM_translator.so", "libGLES_CM.so.1", "libGLES_CM.so"};
Nicolas Capensd76b5df2014-10-28 15:54:46 -040064 #endif
65
66 libGLES_CM = loadLibrary(libGLES_CM_lib);
Nicolas Capens14ee7622014-10-28 23:48:41 -040067 es1::getProcAddress = (__eglMustCastToProperFunctionPointerType (*)(const char*))getProcAddress(libGLES_CM, "glGetProcAddress");
Nicolas Capensd76b5df2014-10-28 15:54:46 -040068
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040069 return libEGL != 0;
John Bauman66b8ab22014-05-06 15:57:45 -040070}
71
72DESTRUCTOR static void glDetachProcess()
73{
74 TRACE("()");
75
76 glDetachThread();
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040077 freeLibrary(libEGL);
Nicolas Capensd76b5df2014-10-28 15:54:46 -040078 freeLibrary(libGLES_CM);
John Bauman66b8ab22014-05-06 15:57:45 -040079}
80
81#if defined(_WIN32)
82extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
83{
84 switch(reason)
85 {
86 case DLL_PROCESS_ATTACH:
87 return glAttachProcess();
88 break;
89 case DLL_THREAD_ATTACH:
90 glAttachThread();
91 break;
92 case DLL_THREAD_DETACH:
93 glDetachThread();
94 break;
95 case DLL_PROCESS_DETACH:
96 glDetachProcess();
97 break;
98 default:
99 break;
100 }
101
102 return TRUE;
103}
104#endif
105
Nicolas Capens14ee7622014-10-28 23:48:41 -0400106namespace es2
John Bauman66b8ab22014-05-06 15:57:45 -0400107{
Nicolas Capens14ee7622014-10-28 23:48:41 -0400108es2::Context *getContext()
John Baumand4ae8632014-05-06 16:18:33 -0400109{
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400110 egl::Context *context = egl::getCurrentContext();
Nicolas Capens8d869e02014-12-08 16:52:06 -0500111
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400112 if(context && context->getClientVersion() == 2)
John Baumand4ae8632014-05-06 16:18:33 -0400113 {
Nicolas Capens14ee7622014-10-28 23:48:41 -0400114 return static_cast<es2::Context*>(context);
John Baumand4ae8632014-05-06 16:18:33 -0400115 }
Nicolas Capens8d869e02014-12-08 16:52:06 -0500116
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400117 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400118}
119
120egl::Display *getDisplay()
121{
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400122 return egl::getCurrentDisplay();
John Bauman66b8ab22014-05-06 15:57:45 -0400123}
124
125Device *getDevice()
126{
Nicolas Capensc78445c2014-10-27 17:29:04 -0400127 Context *context = getContext();
John Bauman66b8ab22014-05-06 15:57:45 -0400128
Nicolas Capensc78445c2014-10-27 17:29:04 -0400129 return context ? context->getDevice() : 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400130}
131}
132
Nicolas Capensd76b5df2014-10-28 15:54:46 -0400133namespace egl
134{
135GLint getClientVersion()
136{
137 Context *context = egl::getCurrentContext();
138
139 return context ? context->getClientVersion() : 0;
140}
141}
142
John Bauman66b8ab22014-05-06 15:57:45 -0400143// Records an error code
144void error(GLenum errorCode)
145{
Nicolas Capens14ee7622014-10-28 23:48:41 -0400146 es2::Context *context = es2::getContext();
John Bauman66b8ab22014-05-06 15:57:45 -0400147
148 if(context)
149 {
150 switch(errorCode)
151 {
152 case GL_INVALID_ENUM:
153 context->recordInvalidEnum();
154 TRACE("\t! Error generated: invalid enum\n");
155 break;
156 case GL_INVALID_VALUE:
157 context->recordInvalidValue();
158 TRACE("\t! Error generated: invalid value\n");
159 break;
160 case GL_INVALID_OPERATION:
161 context->recordInvalidOperation();
162 TRACE("\t! Error generated: invalid operation\n");
163 break;
164 case GL_OUT_OF_MEMORY:
165 context->recordOutOfMemory();
166 TRACE("\t! Error generated: out of memory\n");
167 break;
168 case GL_INVALID_FRAMEBUFFER_OPERATION:
169 context->recordInvalidFramebufferOperation();
170 TRACE("\t! Error generated: invalid framebuffer operation\n");
171 break;
172 default: UNREACHABLE();
173 }
174 }
175}
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400176
177namespace egl
178{
179 egl::Context *(*getCurrentContext)() = 0;
180 egl::Display *(*getCurrentDisplay)() = 0;
181}
182
Nicolas Capens14ee7622014-10-28 23:48:41 -0400183namespace es1
Nicolas Capensd76b5df2014-10-28 15:54:46 -0400184{
185 __eglMustCastToProperFunctionPointerType (*getProcAddress)(const char *procname) = 0;
186}
187
188void *libEGL = 0; // Handle to the libEGL module
189void *libGLES_CM = 0; // Handle to the libGLES_CM module