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