1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // main.cpp: DLLMain and management of thread-local data. 16 17 #include "main.h" 18 19 #if !defined(_MSC_VER) 20 #define CONSTRUCTOR __attribute__((constructor)) 21 #define DESTRUCTOR __attribute__((destructor)) 22 #else 23 #define CONSTRUCTOR 24 #define DESTRUCTOR 25 #endif 26 glAttachThread()27static void glAttachThread() 28 { 29 TRACE("()"); 30 } 31 glDetachThread()32static void glDetachThread() 33 { 34 TRACE("()"); 35 } 36 glAttachProcess()37CONSTRUCTOR static void glAttachProcess() 38 { 39 TRACE("()"); 40 41 glAttachThread(); 42 } 43 glDetachProcess()44DESTRUCTOR static void glDetachProcess() 45 { 46 TRACE("()"); 47 48 glDetachThread(); 49 } 50 51 #if defined(_WIN32) DllMain(HINSTANCE instance,DWORD reason,LPVOID reserved)52extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) 53 { 54 switch(reason) 55 { 56 case DLL_PROCESS_ATTACH: 57 glAttachProcess(); 58 break; 59 case DLL_THREAD_ATTACH: 60 glAttachThread(); 61 break; 62 case DLL_THREAD_DETACH: 63 glDetachThread(); 64 break; 65 case DLL_PROCESS_DETACH: 66 glDetachProcess(); 67 break; 68 default: 69 break; 70 } 71 72 return TRUE; 73 } 74 #endif 75 76 namespace es2 77 { getContextLocked()78Context *getContextLocked() 79 { 80 egl::Context *context = libEGL->clientGetCurrentContext(); 81 82 if(context && (context->getClientVersion() == 2 || 83 context->getClientVersion() == 3)) 84 { 85 return static_cast<es2::Context*>(context); 86 } 87 88 return nullptr; 89 } 90 getContext()91ContextPtr getContext() 92 { 93 return ContextPtr{getContextLocked()}; 94 } 95 getDevice()96Device *getDevice() 97 { 98 Context *context = getContextLocked(); 99 100 return context ? context->getDevice() : nullptr; 101 } 102 103 // Records an error code 104 // Assumed to already hold the context lock for the current context error(GLenum errorCode)105void error(GLenum errorCode) 106 { 107 es2::Context *context = es2::getContextLocked(); 108 109 if(context) 110 { 111 switch(errorCode) 112 { 113 case GL_INVALID_ENUM: 114 context->recordInvalidEnum(); 115 TRACE("\t! Error generated: invalid enum\n"); 116 break; 117 case GL_INVALID_VALUE: 118 context->recordInvalidValue(); 119 TRACE("\t! Error generated: invalid value\n"); 120 break; 121 case GL_INVALID_OPERATION: 122 context->recordInvalidOperation(); 123 TRACE("\t! Error generated: invalid operation\n"); 124 break; 125 case GL_OUT_OF_MEMORY: 126 context->recordOutOfMemory(); 127 TRACE("\t! Error generated: out of memory\n"); 128 break; 129 case GL_INVALID_FRAMEBUFFER_OPERATION: 130 context->recordInvalidFramebufferOperation(); 131 TRACE("\t! Error generated: invalid framebuffer operation\n"); 132 break; 133 default: UNREACHABLE(errorCode); 134 } 135 } 136 } 137 } 138 139 namespace egl 140 { getClientVersion()141GLint getClientVersion() 142 { 143 Context *context = libEGL->clientGetCurrentContext(); 144 145 return context ? context->getClientVersion() : 0; 146 } 147 } 148