blob: a6a0d48e8c5bc4b7d7ae34b21389ebabcda9d033 [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 Capensafd1f9f2014-10-28 02:53:50 -040046 #if defined(_WIN32)
47 const char *libEGL_lib = "libEGL.dll";
48 #else
49 const char *libEGL_lib = "libEGL.so.1";
50 #endif
51
52 libEGL = loadLibrary(libEGL_lib);
53 egl::getCurrentContext = (egl::Context *(*)())getProcAddress(libEGL, "eglGetCurrentContext");
54 egl::getCurrentDisplay = (egl::Display *(*)())getProcAddress(libEGL, "eglGetCurrentDisplay");
55
56 return libEGL != 0;
John Bauman66b8ab22014-05-06 15:57:45 -040057}
58
59DESTRUCTOR static void glDetachProcess()
60{
61 TRACE("()");
62
63 glDetachThread();
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040064 freeLibrary(libEGL);
John Bauman66b8ab22014-05-06 15:57:45 -040065}
66
67#if defined(_WIN32)
68extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
69{
70 switch(reason)
71 {
72 case DLL_PROCESS_ATTACH:
73 return glAttachProcess();
74 break;
75 case DLL_THREAD_ATTACH:
76 glAttachThread();
77 break;
78 case DLL_THREAD_DETACH:
79 glDetachThread();
80 break;
81 case DLL_PROCESS_DETACH:
82 glDetachProcess();
83 break;
84 default:
85 break;
86 }
87
88 return TRUE;
89}
90#endif
91
Nicolas Capensf9b76a82014-10-28 01:55:27 -040092namespace gl2
John Bauman66b8ab22014-05-06 15:57:45 -040093{
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040094gl2::Context *getContext()
John Baumand4ae8632014-05-06 16:18:33 -040095{
Nicolas Capensafd1f9f2014-10-28 02:53:50 -040096 egl::Context *context = egl::getCurrentContext();
97
98 if(context && context->getClientVersion() == 2)
John Baumand4ae8632014-05-06 16:18:33 -040099 {
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400100 return static_cast<gl2::Context*>(context);
John Baumand4ae8632014-05-06 16:18:33 -0400101 }
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400102
103 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400104}
105
106egl::Display *getDisplay()
107{
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400108 return egl::getCurrentDisplay();
John Bauman66b8ab22014-05-06 15:57:45 -0400109}
110
111Device *getDevice()
112{
Nicolas Capensc78445c2014-10-27 17:29:04 -0400113 Context *context = getContext();
John Bauman66b8ab22014-05-06 15:57:45 -0400114
Nicolas Capensc78445c2014-10-27 17:29:04 -0400115 return context ? context->getDevice() : 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400116}
117}
118
119// Records an error code
120void error(GLenum errorCode)
121{
Nicolas Capensf9b76a82014-10-28 01:55:27 -0400122 gl2::Context *context = gl2::getContext();
John Bauman66b8ab22014-05-06 15:57:45 -0400123
124 if(context)
125 {
126 switch(errorCode)
127 {
128 case GL_INVALID_ENUM:
129 context->recordInvalidEnum();
130 TRACE("\t! Error generated: invalid enum\n");
131 break;
132 case GL_INVALID_VALUE:
133 context->recordInvalidValue();
134 TRACE("\t! Error generated: invalid value\n");
135 break;
136 case GL_INVALID_OPERATION:
137 context->recordInvalidOperation();
138 TRACE("\t! Error generated: invalid operation\n");
139 break;
140 case GL_OUT_OF_MEMORY:
141 context->recordOutOfMemory();
142 TRACE("\t! Error generated: out of memory\n");
143 break;
144 case GL_INVALID_FRAMEBUFFER_OPERATION:
145 context->recordInvalidFramebufferOperation();
146 TRACE("\t! Error generated: invalid framebuffer operation\n");
147 break;
148 default: UNREACHABLE();
149 }
150 }
151}
Nicolas Capensafd1f9f2014-10-28 02:53:50 -0400152
153namespace egl
154{
155 egl::Context *(*getCurrentContext)() = 0;
156 egl::Display *(*getCurrentDisplay)() = 0;
157}
158
159void *libEGL = 0; // Handle to the libEGL module