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" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 11 | |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 12 | #include "libGLESv2/Context.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 13 | |
| 14 | static DWORD currentTLS = TLS_OUT_OF_INDEXES; |
| 15 | |
Jamie Madill | bbb43c1 | 2013-12-09 17:04:29 -0500 | [diff] [blame] | 16 | namespace gl |
| 17 | { |
| 18 | |
| 19 | Current *AllocateCurrent() |
| 20 | { |
| 21 | Current *current = (Current*)LocalAlloc(LPTR, sizeof(Current)); |
| 22 | |
| 23 | if (!current) |
| 24 | { |
| 25 | ERR("Could not allocate thread local storage."); |
| 26 | return NULL; |
| 27 | } |
| 28 | |
| 29 | ASSERT(currentTLS != TLS_OUT_OF_INDEXES); |
| 30 | TlsSetValue(currentTLS, current); |
| 31 | |
| 32 | current->context = NULL; |
| 33 | current->display = NULL; |
| 34 | |
| 35 | return current; |
| 36 | } |
| 37 | |
| 38 | void DeallocateCurrent() |
| 39 | { |
| 40 | void *current = TlsGetValue(currentTLS); |
| 41 | |
| 42 | if (current) |
| 43 | { |
| 44 | LocalFree((HLOCAL)current); |
| 45 | } |
| 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 | { |
| 56 | currentTLS = TlsAlloc(); |
| 57 | |
| 58 | if (currentTLS == TLS_OUT_OF_INDEXES) |
| 59 | { |
| 60 | return FALSE; |
| 61 | } |
| 62 | } |
| 63 | // Fall throught to initialize index |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 64 | case DLL_THREAD_ATTACH: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 65 | { |
Jamie Madill | bbb43c1 | 2013-12-09 17:04:29 -0500 | [diff] [blame] | 66 | gl::AllocateCurrent(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 67 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 68 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 69 | case DLL_THREAD_DETACH: |
| 70 | { |
Jamie Madill | bbb43c1 | 2013-12-09 17:04:29 -0500 | [diff] [blame] | 71 | gl::DeallocateCurrent(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 72 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 73 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 74 | case DLL_PROCESS_DETACH: |
| 75 | { |
Jamie Madill | bbb43c1 | 2013-12-09 17:04:29 -0500 | [diff] [blame] | 76 | gl::DeallocateCurrent(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 77 | TlsFree(currentTLS); |
| 78 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 79 | break; |
| 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 84 | return TRUE; |
| 85 | } |
| 86 | |
| 87 | namespace gl |
| 88 | { |
Jamie Madill | bbb43c1 | 2013-12-09 17:04:29 -0500 | [diff] [blame] | 89 | |
| 90 | Current *GetCurrentData() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 91 | { |
| 92 | Current *current = (Current*)TlsGetValue(currentTLS); |
| 93 | |
Jamie Madill | bbb43c1 | 2013-12-09 17:04:29 -0500 | [diff] [blame] | 94 | // ANGLE issue 488: when the dll is loaded after thread initialization, |
| 95 | // thread local storage (current) might not exist yet. |
| 96 | return (current ? current : AllocateCurrent()); |
| 97 | } |
| 98 | |
| 99 | void makeCurrent(Context *context, egl::Display *display, egl::Surface *surface) |
| 100 | { |
| 101 | Current *current = GetCurrentData(); |
| 102 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 103 | current->context = context; |
| 104 | current->display = display; |
| 105 | |
| 106 | if (context && display && surface) |
| 107 | { |
daniel@transgaming.com | ad62987 | 2012-11-28 19:32:06 +0000 | [diff] [blame] | 108 | context->makeCurrent(surface); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | Context *getContext() |
| 113 | { |
Jamie Madill | bbb43c1 | 2013-12-09 17:04:29 -0500 | [diff] [blame] | 114 | Current *current = GetCurrentData(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 115 | |
| 116 | return current->context; |
| 117 | } |
| 118 | |
daniel@transgaming.com | 9d78850 | 2011-11-09 17:46:55 +0000 | [diff] [blame] | 119 | Context *getNonLostContext() |
| 120 | { |
| 121 | Context *context = getContext(); |
| 122 | |
daniel@transgaming.com | 82b2891 | 2011-12-12 21:01:35 +0000 | [diff] [blame] | 123 | if (context) |
| 124 | { |
| 125 | if (context->isContextLost()) |
| 126 | { |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 127 | gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 82b2891 | 2011-12-12 21:01:35 +0000 | [diff] [blame] | 128 | return NULL; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | return context; |
| 133 | } |
| 134 | } |
daniel@transgaming.com | 9d78850 | 2011-11-09 17:46:55 +0000 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
| 137 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 138 | egl::Display *getDisplay() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 139 | { |
Jamie Madill | bbb43c1 | 2013-12-09 17:04:29 -0500 | [diff] [blame] | 140 | Current *current = GetCurrentData(); |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 141 | |
| 142 | return current->display; |
| 143 | } |
| 144 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 145 | // Records an error code |
| 146 | void error(GLenum errorCode) |
| 147 | { |
| 148 | gl::Context *context = glGetCurrentContext(); |
| 149 | |
| 150 | if (context) |
| 151 | { |
| 152 | switch (errorCode) |
| 153 | { |
| 154 | case GL_INVALID_ENUM: |
| 155 | context->recordInvalidEnum(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 156 | TRACE("\t! Error generated: invalid enum\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 157 | break; |
| 158 | case GL_INVALID_VALUE: |
| 159 | context->recordInvalidValue(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 160 | TRACE("\t! Error generated: invalid value\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 161 | break; |
| 162 | case GL_INVALID_OPERATION: |
| 163 | context->recordInvalidOperation(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 164 | TRACE("\t! Error generated: invalid operation\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 165 | break; |
| 166 | case GL_OUT_OF_MEMORY: |
| 167 | context->recordOutOfMemory(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 168 | TRACE("\t! Error generated: out of memory\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 169 | break; |
| 170 | case GL_INVALID_FRAMEBUFFER_OPERATION: |
| 171 | context->recordInvalidFramebufferOperation(); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 172 | TRACE("\t! Error generated: invalid framebuffer operation\n"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 173 | break; |
| 174 | default: UNREACHABLE(); |
| 175 | } |
| 176 | } |
| 177 | } |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 178 | |
| 179 | } |
| 180 | |