blob: 9ab0ed9da47f96f8754ae1752f9de99239820aab [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 Capensb97ad2e2015-02-11 17:40:30 -0500112 if(context && (context->getClientVersion() == 2 ||
113 context->getClientVersion() == 3))
John Baumand4ae8632014-05-06 16:18:33 -0400114 {
Nicolas Capens14ee7622014-10-28 23:48:41 -0400115 return static_cast<es2::Context*>(context);
John Baumand4ae8632014-05-06 16:18:33 -0400116 }
Nicolas Capens8d869e02014-12-08 16:52:06 -0500117
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400118 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400119}
120
121egl::Display *getDisplay()
122{
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400123 return egl::getCurrentDisplay();
John Bauman66b8ab22014-05-06 15:57:45 -0400124}
125
126Device *getDevice()
127{
Nicolas Capensc78445c2014-10-27 17:29:04 -0400128 Context *context = getContext();
John Bauman66b8ab22014-05-06 15:57:45 -0400129
Nicolas Capensc78445c2014-10-27 17:29:04 -0400130 return context ? context->getDevice() : 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400131}
132}
133
Nicolas Capensd76b5df2014-10-28 15:54:46 -0400134namespace egl
135{
136GLint getClientVersion()
137{
138 Context *context = egl::getCurrentContext();
139
140 return context ? context->getClientVersion() : 0;
141}
142}
143
John Bauman66b8ab22014-05-06 15:57:45 -0400144// Records an error code
145void error(GLenum errorCode)
146{
Nicolas Capens14ee7622014-10-28 23:48:41 -0400147 es2::Context *context = es2::getContext();
John Bauman66b8ab22014-05-06 15:57:45 -0400148
149 if(context)
150 {
151 switch(errorCode)
152 {
153 case GL_INVALID_ENUM:
154 context->recordInvalidEnum();
155 TRACE("\t! Error generated: invalid enum\n");
156 break;
157 case GL_INVALID_VALUE:
158 context->recordInvalidValue();
159 TRACE("\t! Error generated: invalid value\n");
160 break;
161 case GL_INVALID_OPERATION:
162 context->recordInvalidOperation();
163 TRACE("\t! Error generated: invalid operation\n");
164 break;
165 case GL_OUT_OF_MEMORY:
166 context->recordOutOfMemory();
167 TRACE("\t! Error generated: out of memory\n");
168 break;
169 case GL_INVALID_FRAMEBUFFER_OPERATION:
170 context->recordInvalidFramebufferOperation();
171 TRACE("\t! Error generated: invalid framebuffer operation\n");
172 break;
173 default: UNREACHABLE();
174 }
175 }
176}
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400177
178namespace egl
179{
180 egl::Context *(*getCurrentContext)() = 0;
181 egl::Display *(*getCurrentDisplay)() = 0;
182}
183
Nicolas Capens14ee7622014-10-28 23:48:41 -0400184namespace es1
Nicolas Capensd76b5df2014-10-28 15:54:46 -0400185{
186 __eglMustCastToProperFunctionPointerType (*getProcAddress)(const char *procname) = 0;
187}
188
189void *libEGL = 0; // Handle to the libEGL module
190void *libGLES_CM = 0; // Handle to the libGLES_CM module