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"
|
| 19 | #include "common/debug.h"
|
| 20 |
|
| 21 | static sw::Thread::LocalStorageKey currentTLS = TLS_OUT_OF_INDEXES;
|
| 22 |
|
| 23 | #if !defined(_MSC_VER)
|
| 24 | #define CONSTRUCTOR __attribute__((constructor))
|
| 25 | #define DESTRUCTOR __attribute__((destructor))
|
| 26 | #else
|
| 27 | #define CONSTRUCTOR
|
| 28 | #define DESTRUCTOR
|
| 29 | #endif
|
| 30 |
|
| 31 | static void glAttachThread()
|
| 32 | {
|
| 33 | TRACE("()");
|
| 34 |
|
Nicolas Capens | f9b76a8 | 2014-10-28 01:55:27 -0400 | [diff] [blame^] | 35 | gl2::Current *current = new gl2::Current;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 36 |
|
| 37 | if(current)
|
| 38 | {
|
| 39 | sw::Thread::setLocalStorage(currentTLS, current);
|
| 40 |
|
| 41 | current->context = NULL;
|
| 42 | current->display = NULL;
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | static void glDetachThread()
|
| 47 | {
|
| 48 | TRACE("()");
|
| 49 |
|
Nicolas Capens | f9b76a8 | 2014-10-28 01:55:27 -0400 | [diff] [blame^] | 50 | gl2::Current *current = (gl2::Current*)sw::Thread::getLocalStorage(currentTLS);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 51 |
|
| 52 | if(current)
|
| 53 | {
|
| 54 | delete current;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | CONSTRUCTOR static bool glAttachProcess()
|
| 59 | {
|
| 60 | TRACE("()");
|
| 61 |
|
| 62 | currentTLS = sw::Thread::allocateLocalStorageKey();
|
| 63 |
|
| 64 | if(currentTLS == TLS_OUT_OF_INDEXES)
|
| 65 | {
|
| 66 | return false;
|
| 67 | }
|
| 68 |
|
| 69 | glAttachThread();
|
| 70 |
|
| 71 | return true;
|
| 72 | }
|
| 73 |
|
| 74 | DESTRUCTOR static void glDetachProcess()
|
| 75 | {
|
| 76 | TRACE("()");
|
| 77 |
|
| 78 | glDetachThread();
|
| 79 |
|
| 80 | sw::Thread::freeLocalStorageKey(currentTLS);
|
| 81 | }
|
| 82 |
|
| 83 | #if defined(_WIN32)
|
| 84 | extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
|
| 85 | {
|
| 86 | switch(reason)
|
| 87 | {
|
| 88 | case DLL_PROCESS_ATTACH:
|
| 89 | return glAttachProcess();
|
| 90 | break;
|
| 91 | case DLL_THREAD_ATTACH:
|
| 92 | glAttachThread();
|
| 93 | break;
|
| 94 | case DLL_THREAD_DETACH:
|
| 95 | glDetachThread();
|
| 96 | break;
|
| 97 | case DLL_PROCESS_DETACH:
|
| 98 | glDetachProcess();
|
| 99 | break;
|
| 100 | default:
|
| 101 | break;
|
| 102 | }
|
| 103 |
|
| 104 | return TRUE;
|
| 105 | }
|
| 106 | #endif
|
| 107 |
|
Nicolas Capens | f9b76a8 | 2014-10-28 01:55:27 -0400 | [diff] [blame^] | 108 | namespace gl2
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 109 | {
|
Nicolas Capens | f9b76a8 | 2014-10-28 01:55:27 -0400 | [diff] [blame^] | 110 | static gl2::Current *getCurrent(void)
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 111 | {
|
| 112 | Current *current = (Current*)sw::Thread::getLocalStorage(currentTLS);
|
| 113 |
|
| 114 | if(!current)
|
| 115 | {
|
| 116 | glAttachThread();
|
| 117 | }
|
| 118 |
|
| 119 | return (Current*)sw::Thread::getLocalStorage(currentTLS);
|
| 120 | }
|
| 121 |
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 122 | void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface)
|
| 123 | {
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 124 | Current *current = getCurrent();
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 125 |
|
| 126 | current->context = context;
|
| 127 | current->display = display;
|
| 128 |
|
| 129 | if(context && display && surface)
|
| 130 | {
|
| 131 | context->makeCurrent(display, surface);
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | Context *getContext()
|
| 136 | {
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 137 | Current *current = getCurrent();
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 138 |
|
| 139 | return current->context;
|
| 140 | }
|
| 141 |
|
| 142 | egl::Display *getDisplay()
|
| 143 | {
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 144 | Current *current = getCurrent();
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 145 |
|
| 146 | return current->display;
|
| 147 | }
|
| 148 |
|
| 149 | Device *getDevice()
|
| 150 | {
|
Nicolas Capens | c78445c | 2014-10-27 17:29:04 -0400 | [diff] [blame] | 151 | Context *context = getContext();
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 152 |
|
Nicolas Capens | c78445c | 2014-10-27 17:29:04 -0400 | [diff] [blame] | 153 | return context ? context->getDevice() : 0;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | // Records an error code
|
| 158 | void error(GLenum errorCode)
|
| 159 | {
|
Nicolas Capens | f9b76a8 | 2014-10-28 01:55:27 -0400 | [diff] [blame^] | 160 | gl2::Context *context = gl2::getContext();
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 161 |
|
| 162 | if(context)
|
| 163 | {
|
| 164 | switch(errorCode)
|
| 165 | {
|
| 166 | case GL_INVALID_ENUM:
|
| 167 | context->recordInvalidEnum();
|
| 168 | TRACE("\t! Error generated: invalid enum\n");
|
| 169 | break;
|
| 170 | case GL_INVALID_VALUE:
|
| 171 | context->recordInvalidValue();
|
| 172 | TRACE("\t! Error generated: invalid value\n");
|
| 173 | break;
|
| 174 | case GL_INVALID_OPERATION:
|
| 175 | context->recordInvalidOperation();
|
| 176 | TRACE("\t! Error generated: invalid operation\n");
|
| 177 | break;
|
| 178 | case GL_OUT_OF_MEMORY:
|
| 179 | context->recordOutOfMemory();
|
| 180 | TRACE("\t! Error generated: out of memory\n");
|
| 181 | break;
|
| 182 | case GL_INVALID_FRAMEBUFFER_OPERATION:
|
| 183 | context->recordInvalidFramebufferOperation();
|
| 184 | TRACE("\t! Error generated: invalid framebuffer operation\n");
|
| 185 | break;
|
| 186 | default: UNREACHABLE();
|
| 187 | }
|
| 188 | }
|
| 189 | }
|