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 | |
| 14 | static TLSIndex currentTLS = TLS_OUT_OF_INDEXES; |
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 | |
| 19 | Current *AllocateCurrent() |
| 20 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 21 | ASSERT(currentTLS != TLS_OUT_OF_INDEXES); |
| 22 | if (currentTLS == TLS_OUT_OF_INDEXES) |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 23 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 24 | return NULL; |
| 25 | } |
| 26 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 27 | Current *current = new Current(); |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 28 | current->context = NULL; |
| 29 | current->display = NULL; |
| 30 | |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 31 | if (!SetTLSValue(currentTLS, current)) |
| 32 | { |
| 33 | ERR("Could not set thread local storage."); |
| 34 | return NULL; |
| 35 | } |
| 36 | |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 37 | return current; |
| 38 | } |
| 39 | |
| 40 | void DeallocateCurrent() |
| 41 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 42 | Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS)); |
| 43 | SafeDelete(current); |
| 44 | SetTLSValue(currentTLS, NULL); |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | } |
| 48 | |
daniel@transgaming.com | fad16ed | 2012-10-17 18:24:01 +0000 | [diff] [blame] | 49 | extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | { |
| 51 | switch (reason) |
| 52 | { |
| 53 | case DLL_PROCESS_ATTACH: |
| 54 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 55 | currentTLS = CreateTLSIndex(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 56 | if (currentTLS == TLS_OUT_OF_INDEXES) |
| 57 | { |
| 58 | return FALSE; |
| 59 | } |
| 60 | } |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 61 | // Fall through to initialize index |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 62 | case DLL_THREAD_ATTACH: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 63 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 64 | gl::AllocateCurrent(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 65 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 66 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 67 | case DLL_THREAD_DETACH: |
| 68 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 69 | gl::DeallocateCurrent(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 70 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 71 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 72 | case DLL_PROCESS_DETACH: |
| 73 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 74 | gl::DeallocateCurrent(); |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 75 | DestroyTLSIndex(currentTLS); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 76 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 77 | break; |
| 78 | default: |
| 79 | break; |
| 80 | } |
| 81 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 82 | return TRUE; |
| 83 | } |
| 84 | |
| 85 | namespace gl |
| 86 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 87 | |
| 88 | Current *GetCurrentData() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 89 | { |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 90 | Current *current = reinterpret_cast<Current*>(GetTLSValue(currentTLS)); |
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 | // ANGLE issue 488: when the dll is loaded after thread initialization, |
| 93 | // thread local storage (current) might not exist yet. |
| 94 | return (current ? current : AllocateCurrent()); |
| 95 | } |
| 96 | |
| 97 | void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface) |
| 98 | { |
| 99 | Current *current = GetCurrentData(); |
| 100 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 101 | current->context = context; |
| 102 | current->display = display; |
| 103 | |
| 104 | if (context && display && surface) |
| 105 | { |
daniel@transgaming.com | ad62987 | 2012-11-28 19:32:06 +0000 | [diff] [blame] | 106 | context->makeCurrent(surface); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | Context *getContext() |
| 111 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 112 | Current *current = GetCurrentData(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 113 | |
| 114 | return current->context; |
| 115 | } |
| 116 | |
daniel@transgaming.com | 9d78850 | 2011-11-09 17:46:55 +0000 | [diff] [blame] | 117 | Context *getNonLostContext() |
| 118 | { |
| 119 | Context *context = getContext(); |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 120 | |
daniel@transgaming.com | 82b2891 | 2011-12-12 21:01:35 +0000 | [diff] [blame] | 121 | if (context) |
| 122 | { |
| 123 | if (context->isContextLost()) |
| 124 | { |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 125 | gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 82b2891 | 2011-12-12 21:01:35 +0000 | [diff] [blame] | 126 | return NULL; |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | return context; |
| 131 | } |
| 132 | } |
daniel@transgaming.com | 9d78850 | 2011-11-09 17:46:55 +0000 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
| 135 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 136 | egl::Display *getDisplay() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 137 | { |
Jamie Madill | 7a93437 | 2013-12-06 18:17:48 -0500 | [diff] [blame] | 138 | Current *current = GetCurrentData(); |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 139 | |
| 140 | return current->display; |
| 141 | } |
| 142 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 143 | // Records an error code |
| 144 | void error(GLenum errorCode) |
| 145 | { |
| 146 | gl::Context *context = glGetCurrentContext(); |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 147 | context->recordError(Error(errorCode)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 148 | |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 149 | switch (errorCode) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 150 | { |
Geoff Lang | da5777c | 2014-07-11 09:52:58 -0400 | [diff] [blame] | 151 | case GL_INVALID_ENUM: |
| 152 | TRACE("\t! Error generated: invalid enum\n"); |
| 153 | break; |
| 154 | case GL_INVALID_VALUE: |
| 155 | TRACE("\t! Error generated: invalid value\n"); |
| 156 | break; |
| 157 | case GL_INVALID_OPERATION: |
| 158 | TRACE("\t! Error generated: invalid operation\n"); |
| 159 | break; |
| 160 | case GL_OUT_OF_MEMORY: |
| 161 | TRACE("\t! Error generated: out of memory\n"); |
| 162 | break; |
| 163 | case GL_INVALID_FRAMEBUFFER_OPERATION: |
| 164 | TRACE("\t! Error generated: invalid framebuffer operation\n"); |
| 165 | break; |
| 166 | default: UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 169 | |
| 170 | } |
| 171 | |