Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 | |
Corentin Wallez | b039711 | 2017-10-26 19:27:22 -0400 | [diff] [blame] | 15 | // main.cpp: DLLMain and management of thread-local data. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 16 | |
| 17 | #include "main.h" |
| 18 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 | |
| 27 | static void glAttachThread() |
| 28 | { |
| 29 | TRACE("()"); |
| 30 | } |
| 31 | |
| 32 | static void glDetachThread() |
| 33 | { |
| 34 | TRACE("()"); |
| 35 | } |
| 36 | |
| 37 | CONSTRUCTOR static void glAttachProcess() |
| 38 | { |
| 39 | TRACE("()"); |
| 40 | |
| 41 | glAttachThread(); |
| 42 | } |
| 43 | |
| 44 | DESTRUCTOR static void glDetachProcess() |
| 45 | { |
| 46 | TRACE("()"); |
| 47 | |
| 48 | glDetachThread(); |
| 49 | } |
| 50 | |
| 51 | #if defined(_WIN32) |
| 52 | extern "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 | { |
Chris Forbes | 108f3e1 | 2018-08-30 19:41:59 -0700 | [diff] [blame^] | 78 | Context *getContextLocked() |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 | |
Nicolas Capens | 81aa97b | 2017-06-27 17:08:08 -0400 | [diff] [blame] | 88 | return nullptr; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Chris Forbes | 108f3e1 | 2018-08-30 19:41:59 -0700 | [diff] [blame^] | 91 | ContextPtr getContext() |
| 92 | { |
| 93 | return ContextPtr{getContextLocked()}; |
| 94 | } |
| 95 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 96 | Device *getDevice() |
| 97 | { |
Chris Forbes | 108f3e1 | 2018-08-30 19:41:59 -0700 | [diff] [blame^] | 98 | Context *context = getContextLocked(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 99 | |
Nicolas Capens | 81aa97b | 2017-06-27 17:08:08 -0400 | [diff] [blame] | 100 | return context ? context->getDevice() : nullptr; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // Records an error code |
Chris Forbes | 108f3e1 | 2018-08-30 19:41:59 -0700 | [diff] [blame^] | 104 | // Assumed to already hold the context lock for the current context |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 105 | void error(GLenum errorCode) |
| 106 | { |
Chris Forbes | 108f3e1 | 2018-08-30 19:41:59 -0700 | [diff] [blame^] | 107 | es2::Context *context = es2::getContextLocked(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 | { |
| 141 | GLint getClientVersion() |
| 142 | { |
| 143 | Context *context = libEGL->clientGetCurrentContext(); |
| 144 | |
| 145 | return context ? context->getClientVersion() : 0; |
| 146 | } |
| 147 | } |