John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1 | // SwiftShader Software Renderer
|
| 2 | //
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 3 | // Copyright(c) 2005-2013 TransGaming Inc.
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 4 | //
|
| 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 Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 19 | #include "Common/SharedLibrary.hpp"
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 20 | #include "common/debug.h"
|
| 21 |
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 22 | #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 |
|
| 30 | static void glAttachThread()
|
| 31 | {
|
| 32 | TRACE("()");
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 33 | }
|
| 34 |
|
| 35 | static void glDetachThread()
|
| 36 | {
|
| 37 | TRACE("()");
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 38 | }
|
| 39 |
|
| 40 | CONSTRUCTOR static bool glAttachProcess()
|
| 41 | {
|
| 42 | TRACE("()");
|
| 43 |
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 44 | glAttachThread();
|
| 45 |
|
Nicolas Capens | e254010 | 2014-11-05 09:34:52 -0500 | [diff] [blame] | 46 | #if defined(_WIN32)
|
| 47 | const char *libEGL_lib[] = {"libEGL.dll", "libEGL_translator.dll"};
|
Nicolas Capens | 4cadfe3 | 2014-12-10 22:26:26 -0500 | [diff] [blame] | 48 | #elif defined(__LP64__)
|
| 49 | const char *libEGL_lib[] = {"lib64EGL_translator.so", "libEGL.so.1", "libEGL.so"};
|
Nicolas Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 50 | #else
|
Nicolas Capens | 8d869e0 | 2014-12-08 16:52:06 -0500 | [diff] [blame] | 51 | const char *libEGL_lib[] = {"libEGL_translator.so", "libEGL.so.1", "libEGL.so"};
|
Nicolas Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 52 | #endif
|
| 53 |
|
| 54 | libEGL = loadLibrary(libEGL_lib);
|
Nicolas Capens | 53e3886 | 2014-11-05 16:05:12 -0500 | [diff] [blame] | 55 | egl::getCurrentContext = (egl::Context *(*)())getProcAddress(libEGL, "clientGetCurrentContext");
|
| 56 | egl::getCurrentDisplay = (egl::Display *(*)())getProcAddress(libEGL, "clientGetCurrentDisplay");
|
Nicolas Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 57 |
|
Nicolas Capens | d76b5df | 2014-10-28 15:54:46 -0400 | [diff] [blame] | 58 | #if defined(_WIN32)
|
Nicolas Capens | e254010 | 2014-11-05 09:34:52 -0500 | [diff] [blame] | 59 | const char *libGLES_CM_lib[] = {"libGLES_CM.dll", "libGLES_CM_translator.dll"};
|
Nicolas Capens | 4cadfe3 | 2014-12-10 22:26:26 -0500 | [diff] [blame] | 60 | #elif defined(__LP64__)
|
| 61 | const char *libGLES_CM_lib[] = {"lib64GLES_CM_translator.so", "libGLES_CM.so.1", "libGLES_CM.so"};
|
Nicolas Capens | d76b5df | 2014-10-28 15:54:46 -0400 | [diff] [blame] | 62 | #else
|
Nicolas Capens | 8d869e0 | 2014-12-08 16:52:06 -0500 | [diff] [blame] | 63 | const char *libGLES_CM_lib[] = {"libGLES_CM_translator.so", "libGLES_CM.so.1", "libGLES_CM.so"};
|
Nicolas Capens | d76b5df | 2014-10-28 15:54:46 -0400 | [diff] [blame] | 64 | #endif
|
| 65 |
|
| 66 | libGLES_CM = loadLibrary(libGLES_CM_lib);
|
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 67 | es1::getProcAddress = (__eglMustCastToProperFunctionPointerType (*)(const char*))getProcAddress(libGLES_CM, "glGetProcAddress");
|
Nicolas Capens | d76b5df | 2014-10-28 15:54:46 -0400 | [diff] [blame] | 68 |
|
Nicolas Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 69 | return libEGL != 0;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 70 | }
|
| 71 |
|
| 72 | DESTRUCTOR static void glDetachProcess()
|
| 73 | {
|
| 74 | TRACE("()");
|
| 75 |
|
| 76 | glDetachThread();
|
Nicolas Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 77 | freeLibrary(libEGL);
|
Nicolas Capens | d76b5df | 2014-10-28 15:54:46 -0400 | [diff] [blame] | 78 | freeLibrary(libGLES_CM);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 79 | }
|
| 80 |
|
| 81 | #if defined(_WIN32)
|
| 82 | extern "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 Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 106 | namespace es2
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 107 | {
|
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 108 | es2::Context *getContext()
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 109 | {
|
Nicolas Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 110 | egl::Context *context = egl::getCurrentContext();
|
Nicolas Capens | 8d869e0 | 2014-12-08 16:52:06 -0500 | [diff] [blame] | 111 |
|
Nicolas Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 112 | if(context && context->getClientVersion() == 2)
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 113 | {
|
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 114 | return static_cast<es2::Context*>(context);
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 115 | }
|
Nicolas Capens | 8d869e0 | 2014-12-08 16:52:06 -0500 | [diff] [blame] | 116 |
|
Nicolas Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 117 | return 0;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 118 | }
|
| 119 |
|
| 120 | egl::Display *getDisplay()
|
| 121 | {
|
Nicolas Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 122 | return egl::getCurrentDisplay();
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 123 | }
|
| 124 |
|
| 125 | Device *getDevice()
|
| 126 | {
|
Nicolas Capens | c78445c | 2014-10-27 17:29:04 -0400 | [diff] [blame] | 127 | Context *context = getContext();
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 128 |
|
Nicolas Capens | c78445c | 2014-10-27 17:29:04 -0400 | [diff] [blame] | 129 | return context ? context->getDevice() : 0;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 130 | }
|
| 131 | }
|
| 132 |
|
Nicolas Capens | d76b5df | 2014-10-28 15:54:46 -0400 | [diff] [blame] | 133 | namespace egl
|
| 134 | {
|
| 135 | GLint getClientVersion()
|
| 136 | {
|
| 137 | Context *context = egl::getCurrentContext();
|
| 138 |
|
| 139 | return context ? context->getClientVersion() : 0;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 143 | // Records an error code
|
| 144 | void error(GLenum errorCode)
|
| 145 | {
|
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 146 | es2::Context *context = es2::getContext();
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 147 |
|
| 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 Capens | afd1f9f | 2014-10-28 02:53:50 -0400 | [diff] [blame] | 176 |
|
| 177 | namespace egl
|
| 178 | {
|
| 179 | egl::Context *(*getCurrentContext)() = 0;
|
| 180 | egl::Display *(*getCurrentDisplay)() = 0;
|
| 181 | }
|
| 182 |
|
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 183 | namespace es1
|
Nicolas Capens | d76b5df | 2014-10-28 15:54:46 -0400 | [diff] [blame] | 184 | {
|
| 185 | __eglMustCastToProperFunctionPointerType (*getProcAddress)(const char *procname) = 0;
|
| 186 | }
|
| 187 |
|
| 188 | void *libEGL = 0; // Handle to the libEGL module
|
| 189 | void *libGLES_CM = 0; // Handle to the libGLES_CM module
|