daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | fad16ed | 2012-10-17 18:24:01 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // main.cpp: DLL entry point and management of thread-local data. |
| 8 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 9 | #include "libGLESv2/main.h" |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 10 | #include "libGLESv2/Context.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 11 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 12 | #include "common/tls.h" |
| 13 | |
Kenneth Russell | 9d6d498 | 2014-10-10 19:47:34 -0700 | [diff] [blame] | 14 | static TLSIndex currentTLS = TLS_INVALID_INDEX; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 15 | |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 16 | namespace gl |
| 17 | { |
| 18 | |
Kenneth Russell | 9d6d498 | 2014-10-10 19:47:34 -0700 | [diff] [blame] | 19 | // TODO(kbr): figure out how these are going to be managed on |
| 20 | // non-Windows platforms. These routines would need to be exported |
| 21 | // from ANGLE and called cooperatively when users create and destroy |
| 22 | // threads -- or the initialization of the TLS index, and allocation |
| 23 | // of thread-local data, will have to be done lazily. Will have to use |
| 24 | // destructor function with pthread_create_key on POSIX platforms to |
| 25 | // clean up thread-local data. |
| 26 | |
| 27 | // Call this exactly once at process startup. |
| 28 | bool CreateThreadLocalIndex() |
| 29 | { |
| 30 | currentTLS = CreateTLSIndex(); |
| 31 | if (currentTLS == TLS_INVALID_INDEX) |
| 32 | { |
| 33 | return false; |
| 34 | } |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | // Call this exactly once at process shutdown. |
| 39 | void DestroyThreadLocalIndex() |
| 40 | { |
| 41 | DestroyTLSIndex(currentTLS); |
| 42 | currentTLS = TLS_INVALID_INDEX; |
| 43 | } |
| 44 | |
| 45 | // Call this upon thread startup. |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 46 | Current *AllocateCurrent() |
| 47 | { |
Kenneth Russell | 9d6d498 | 2014-10-10 19:47:34 -0700 | [diff] [blame] | 48 | ASSERT(currentTLS != TLS_INVALID_INDEX); |
| 49 | if (currentTLS == TLS_INVALID_INDEX) |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 50 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 51 | return NULL; |
| 52 | } |
| 53 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 54 | Current *current = new Current(); |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 55 | current->context = NULL; |
| 56 | current->display = NULL; |
| 57 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 58 | if (!SetTLSValue(currentTLS, current)) |
| 59 | { |
| 60 | ERR("Could not set thread local storage."); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 64 | return current; |
| 65 | } |
| 66 | |
Kenneth Russell | 9d6d498 | 2014-10-10 19:47:34 -0700 | [diff] [blame] | 67 | // Call this upon thread shutdown. |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 68 | void DeallocateCurrent() |
| 69 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 70 | Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS)); |
| 71 | SafeDelete(current); |
| 72 | SetTLSValue(currentTLS, NULL); |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | } |
| 76 | |
Kenneth Russell | 9d6d498 | 2014-10-10 19:47:34 -0700 | [diff] [blame] | 77 | #ifdef ANGLE_PLATFORM_WINDOWS |
daniel@transgaming.com | fad16ed | 2012-10-17 18:24:01 +0000 | [diff] [blame] | 78 | extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 79 | { |
| 80 | switch (reason) |
| 81 | { |
| 82 | case DLL_PROCESS_ATTACH: |
| 83 | { |
Kenneth Russell | 9d6d498 | 2014-10-10 19:47:34 -0700 | [diff] [blame] | 84 | if (!gl::CreateThreadLocalIndex()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 85 | { |
| 86 | return FALSE; |
| 87 | } |
| 88 | } |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 89 | // Fall through to initialize index |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 90 | case DLL_THREAD_ATTACH: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 91 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 92 | gl::AllocateCurrent(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 93 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 94 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 95 | case DLL_THREAD_DETACH: |
| 96 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 97 | gl::DeallocateCurrent(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 98 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 99 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 100 | case DLL_PROCESS_DETACH: |
| 101 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 102 | gl::DeallocateCurrent(); |
Kenneth Russell | 9d6d498 | 2014-10-10 19:47:34 -0700 | [diff] [blame] | 103 | gl::DestroyThreadLocalIndex(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 104 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 105 | break; |
| 106 | default: |
| 107 | break; |
| 108 | } |
| 109 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 110 | return TRUE; |
| 111 | } |
Kenneth Russell | 9d6d498 | 2014-10-10 19:47:34 -0700 | [diff] [blame] | 112 | #endif |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 113 | |
| 114 | namespace gl |
| 115 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 116 | |
| 117 | Current *GetCurrentData() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 118 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 119 | Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 120 | |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 121 | // ANGLE issue 488: when the dll is loaded after thread initialization, |
| 122 | // thread local storage (current) might not exist yet. |
| 123 | return (current ? current : AllocateCurrent()); |
| 124 | } |
| 125 | |
| 126 | void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface) |
| 127 | { |
| 128 | Current *current = GetCurrentData(); |
| 129 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 130 | current->context = context; |
| 131 | current->display = display; |
| 132 | |
| 133 | if (context && display && surface) |
| 134 | { |
daniel@transgaming.com | ad62987 | 2012-11-28 19:32:06 +0000 | [diff] [blame] | 135 | context->makeCurrent(surface); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | Context *getContext() |
| 140 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 141 | Current *current = GetCurrentData(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 142 | |
| 143 | return current->context; |
| 144 | } |
| 145 | |
daniel@transgaming.com | 9d78850 | 2011-11-09 17:46:55 +0000 | [diff] [blame] | 146 | Context *getNonLostContext() |
| 147 | { |
| 148 | Context *context = getContext(); |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 149 | |
daniel@transgaming.com | 82b2891 | 2011-12-12 21:01:35 +0000 | [diff] [blame] | 150 | if (context) |
| 151 | { |
| 152 | if (context->isContextLost()) |
| 153 | { |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 154 | gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 82b2891 | 2011-12-12 21:01:35 +0000 | [diff] [blame] | 155 | return NULL; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | return context; |
| 160 | } |
| 161 | } |
daniel@transgaming.com | 9d78850 | 2011-11-09 17:46:55 +0000 | [diff] [blame] | 162 | return NULL; |
| 163 | } |
| 164 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 165 | egl::Display *getDisplay() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 166 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 167 | Current *current = GetCurrentData(); |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 168 | |
| 169 | return current->display; |
| 170 | } |
| 171 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 172 | // Records an error code |
| 173 | void error(GLenum errorCode) |
| 174 | { |
| 175 | gl::Context *context = glGetCurrentContext(); |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 176 | context->recordError(Error(errorCode)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 177 | |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 178 | switch (errorCode) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | { |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 180 | case GL_INVALID_ENUM: |
| 181 | TRACE("\t! Error generated: invalid enum\n"); |
| 182 | break; |
| 183 | case GL_INVALID_VALUE: |
| 184 | TRACE("\t! Error generated: invalid value\n"); |
| 185 | break; |
| 186 | case GL_INVALID_OPERATION: |
| 187 | TRACE("\t! Error generated: invalid operation\n"); |
| 188 | break; |
| 189 | case GL_OUT_OF_MEMORY: |
| 190 | TRACE("\t! Error generated: out of memory\n"); |
| 191 | break; |
| 192 | case GL_INVALID_FRAMEBUFFER_OPERATION: |
| 193 | TRACE("\t! Error generated: invalid framebuffer operation\n"); |
| 194 | break; |
| 195 | default: UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 198 | |
| 199 | } |